-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-list-html5.html
118 lines (104 loc) · 3.61 KB
/
sort-list-html5.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Drag Element</title>
<style>
.sortablelist {
background-color: aqua;
}
.sortablelist li {
padding-top: 2px;
padding-bottom: 2px;
transition: all .2s linear;
border: 1px solid;
width:100px;
}
.placeholder-hover {
padding-top: 10px !important;
padding-bottom: 10px !important;
transition: all .2s linear;
}
</style>
</head>
<body>
<ul class="sortablelist" id='sortablelist'>
<li id='1' class='item'>1111111111
</li>
<li id='2' class='item'>2222222222
</li>
<li id='3' class='item'>3333333333
</li>
<li id='4' class='item'>4444444444
</li>
<li id='5' class='item'>5555555555
</li>
<li id='6' class='item'>6666666666
</li>
<li id='7' class='item'>7777777777
</li>
<li id='8' class='item'>8888888888
</li>
<li id='9' class='item'>9999999999
</li>
</ul>
<script type="text/javascript">
isDrag = false
function drop(e) {
e.preventDefault();
var data = e.dataTransfer.getData("text");
li = e.target
newitem = document.getElementById(data)
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
//console.log('y',y,'height/2',rect.height/2)
if (y> (rect.height/2)){
//console.log('append drop')
li.parentNode.insertBefore(newitem, li.nextElementSibling);
}
else{
//console.log('insertBefore drop')
li.parentNode.insertBefore(newitem, li);
}
if (e.target.classList.contains('placeholder-hover')) {
e.target.classList.remove('placeholder-hover');
}
}
function dragover(e) {
e.preventDefault();
if (e.target.classList.contains('item')) {
if (!e.target.classList.contains('placeholder-hover')) {
e.target.classList.add('placeholder-hover');
}
return true
}
return false
}
function dragleave(e) {
if (e.target.classList.contains('placeholder-hover')) {
e.target.classList.remove('placeholder-hover');
}
}
function dragend(e) {
isDrag = false;
}
function dragstart(e) {
isDrag = true;
var crt = document.elementFromPoint(e.pageX, e.pageY);
e.dataTransfer.setData("text/plain", crt.id);
e.dataTransfer.setDragImage(crt, 0, 0);
}
Array.prototype.forEach.call(document.getElementById('sortablelist').getElementsByClassName('item'), (el, index) => {
el.addEventListener('dragstart', dragstart, false)
el.setAttribute('draggable', true);
el.addEventListener('drop', drop, false)
el.addEventListener('dragover', dragover, false)
el.addEventListener('dragend', dragend)
el.addEventListener('dragleave', dragleave)
})
</script>
</body>
</html>