-
Notifications
You must be signed in to change notification settings - Fork 193
/
drag-drop.html
97 lines (79 loc) · 1.78 KB
/
drag-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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Drag And Drop</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
font-size: 20px;
padding: 0 20px;
line-height: 1.4;
}
main {
text-align: center;
margin: 0 auto;
max-width: 800px;
}
p {
text-align: left;
padding: 0 20px;
}
code {
color: firebrick;
}
.box {
width: 200px;
height: 200px;
background: thistle;
float: left;
border-radius: 20px;
}
.dropzone {
width: 400px;
height: 400px;
background: deepskyblue;
float: right;
padding: 20px;
border-radius: 20px;
}
.draggable {
cursor: move;
}
.droppable {
background-color: black;
}
</style>
</head>
<body>
<main>
<h2>Drag and Drop API <code>setDragImage()</code></h2>
<div class="box draggable" id="box" draggable="true"></div>
<div class="dropzone" id="dropzone"></div>
</main>
<script>
const box = document.getElementById('box')
const dropzone = document.getElementById('dropzone')
const img = new Image();
img.src = 'https://img.alicdn.com/tfs/TB1nP7.QiLaK1RjSZFxXXamPFXa-256-256.png';
box.addEventListener('dragstart', e => {
e.dataTransfer.setDragImage(img, 92, 92);
});
dropzone.addEventListener('dragover', e => {
e.preventDefault();
e.target.classList.add('droppable');
});
dropzone.addEventListener('drop', e => {
e.target.appendChild(box);
box.draggable = false;
box.classList.remove('draggable');
});
box.addEventListener('dragend', _ => {
dropzone.classList.remove('droppable');
});
</script>
</body>
</html>