-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragndrop.html
91 lines (69 loc) · 1.96 KB
/
dragndrop.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 & Drop</title>
<style>
.drag {
width: 100px;
height: 100px;
background: gold;
margin-right: 10px;
display: inline-block;
}
#drop {
width: 100%;
height: 70%;
background: grey;
position: fixed;
bottom: 0px;
transition: 300ms;
}
#drop.ok { background: lightgreen; }
#drop.error { background: tomato; }
</style>
</head>
<body>
<div class="drag" draggable="true" data-movie-id="A">A</div>
<div class="drag" draggable="true" data-movie-id="B">B</div>
<div class="drag" draggable="true" data-movie-id="C">C</div>
<div class="drag" draggable="true" data-movie-id="D">D</div>
<div id="drop"></div>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script>
var draggedMovieId = null; // definimos variable global como ñapa del dataTranfer no soportado en chrome en eventos dragover, dragleave...
$(".drag").on("dragstart", function(evt){
console.log("DRAG START", evt);
// ponemos id de la serie seleccionada
draggedMovieId = $(this).data("movieId");
evt.originalEvent.dataTransfer.setData("movieId", draggedMovieId);
}).on("dragend", function(evt){
console.log("DRAG STOP", evt);
draggedMovieId = null;
});
$("#drop").on("dragover", function(evt){
evt.preventDefault();
console.log("DRAG OVER", draggedMovieId, evt);
if (draggedMovieId != "B") {
$(this).addClass("ok");
} else {
$(this).addClass("error");
}
}).on("dragleave", function(evt){
evt.preventDefault();
console.log("DRAG LEAVE", evt);
$(this).removeClass("ok error");
}).on("drop", function(evt){
evt.preventDefault();
console.log("DROPPED", draggedMovieId);
var movieId = evt.originalEvent.dataTransfer.getData("movieId");
if (movieId != "B") {
var text = $(this).text();
text += movieId;
$(this).text(text);
}
$(this).removeClass("ok error");
});
</script>
</body>
</html>