forked from eracom/exemples-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrag-and-drop.html
100 lines (83 loc) · 2.83 KB
/
drag-and-drop.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
92
93
94
95
96
97
98
99
100
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>drag and drop</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: "Roboto Mono", monospace;
}
body {
margin: 0;
background: paleturquoise;
}
main {
border: 1px solid blue;
height: 90vh;
margin: 5vh;
display: flex;
}
section {
width: 50%;
border: 1px solid grey;
padding: 1em;
background: papayawhip;
}
p {
border: 1px solid magenta;
padding: 1em;
background: lavender;
}
.dropzone {
border: 1px dotted grey;
background: whitesmoke;
min-height: 5em;
margin-bottom: 1em;
}
</style>
</head>
<body>
<main>
<section class="elements">
<h1>elements</h1>
<p id="drag1">element 1</p>
<p id="drag2">element 2</p>
<p id="drag3">element 3</p>
</section>
<section class="dragcontainer">
<h1>drag zone</h1>
<div id="dropzone1" class="dropzone" ondrop="drop_handler(event);" ondragover="dragover_handler(event);"></div>
<div id="dropzone2" class="dropzone" ondrop="drop_handler(event);" ondragover="dragover_handler(event);"></div>
</section>
</main>
<script>
// Source: https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
// Rendre les éléments "draggable":
var items = document.querySelectorAll(".elements>p");
var i;
for (i = 0; i < items.length; i++) {
items[i].setAttribute('draggable', true);
items[i].addEventListener('dragstart', function(){dragstart_handler(event)}, false);
}
function dragstart_handler(ev) {
// console.log("dragStart");
// Add the target element's id to the data transfer object
ev.dataTransfer.setData("text/plain", ev.target.id);
ev.dropEffect = "move";
}
function dragover_handler(ev) {
ev.preventDefault();
// Set the dropEffect to move
ev.dataTransfer.dropEffect = "move";
}
function drop_handler(ev) {
ev.preventDefault();
// Get the id of the target and add the moved element to the target's DOM
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
console.log("moved "+data+ " to "+ev.target.id);
}
</script>
</body>
</html>