-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragdrop.html
More file actions
64 lines (56 loc) · 1.84 KB
/
dragdrop.html
File metadata and controls
64 lines (56 loc) · 1.84 KB
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
<!doctype html>
<html lang=ja>
<head>
<title></title>
<meta charset=utf-8>
<style>
div#drag {
width: 100px;
height: 100px;
background-color: #F00;
}
div#drop {
width: 300px;
height: 300px;
background-color: #999999;
}
</style>
</head>
<body>
<!-- ドラッグ&ドロップのの処理 -->
<script type="text/javascript">
window.addEventListener("load", function() {
var dragElm = document.getElementById("drag");
var dropElm = document.getElementById("drop");
dragElm.addEventListener("dragstart", function(event) {
event.dataTransfer.setData("text", event.target.id);
}, true);
dragElm.addEventListener("drag", function(event) {
event.target.style.backgroundColor = "#ffa500";
}, true);
dragElm.addEventListener("dragend", function(event) {
event.target.style.backgroundColor = "#ff0000";
}, true);
dropElm.addEventListener("dragenter", function(event) {
dropElm.style.backgroundColor = "#c0c0c0";
event.preventDefault();
}, true);
dropElm.addEventListener("dragover", function(event) {
dropElm.style.backgroundColor = "#c0c0c0";
event.preventDefault();
}, true);
dropElm.addEventListener("dragleave", function(event) {
dropElm.style.backgroundColor = "#696969";
}, true);
dropElm.addEventListener("drop", function(event) {
var id = event.dataTransfer.getData("text");
var elm = document.getElementById(id);
event.currentTarget.appendChild(elm);
event.preventDefault();
}, true);
}, true);
</script>
<div id="drag" draggable="true">ドラッグ可能です</div>
<div id="drop"></div>
</body>
</html>