-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.html
executable file
·91 lines (79 loc) · 1.98 KB
/
drag.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>drag</title>
<style>
/**
* Drag and Drop Basic
**/
[draggable="true"] {
/*
To prevent user selecting inside the drag source
*/
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#drag-drop-basic{
display: flex;
}
#drag-drop-basic #source-container{
height: 400px;
border: 2px solid #CCC;
flex: 1;
}
#drag-drop-basic #target-container{
height: 400px;
border: 2px solid #CCC;
flex: 1;
}
#drag-drop-basic #drag-source{
width: 100px;
height: 100px;
border-radius: 50%;
background-color: steelblue;
}
</style>
</head>
<body>
<div id="drag-drop-basic">
<div id="source-container" data-role="drag-drop-container">
<div id="drag-source" draggable="true"></div>
</div>
<div id="target-container" data-role="drag-drop-container"></div>
</div>
<script>
// Allow multiple draggable items
let dragSources = document.querySelectorAll('[draggable="true"]')
dragSources.forEach(dragSource => {
dragSource.addEventListener('dragstart', dragStart)
})
function dragStart(e) {
e.dataTransfer.setData('text/plain', e.target.id)
}
// Allow multiple dropped targets
let dropTargets = document.querySelectorAll('[data-role="drag-drop-container"]')
dropTargets.forEach(dropTarget => {
dropTarget.addEventListener('drop', dropped)
dropTarget.addEventListener('dragenter', cancelDefault)
dropTarget.addEventListener('dragover', cancelDefault)
})
function dropped(e) {
cancelDefault(e)
let id = e.dataTransfer.getData('text/plain')
e.target.appendChild(document.querySelector('#' + id))
}
function cancelDefault(e) {
e.preventDefault()
e.stopPropagation()
return false
}
</script>
<script type="module">
import index from './index.js'
console.log('logA: ', index);
</script>
</body>
</html>