-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
44 lines (35 loc) · 995 Bytes
/
script.js
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
let items = document.querySelectorAll('#items-list > img')
items.forEach(item => {
$(item).prop('draggable', true)
item.addEventListener('dragstart', dragStart)
item.addEventListener('drop', dropped)
item.addEventListener('dragenter', cancelDefault)
item.addEventListener('dragover', cancelDefault)
})
function dragStart (e) {
var index = $(e.target).index()
e.dataTransfer.setData('text/plain', index)
}
function dropped (e) {
cancelDefault(e)
// get new and old index
let oldIndex = e.dataTransfer.getData('text/plain')
let target = $(e.target)
let newIndex = target.index()
if (newIndex == oldIndex) {
return
}
// remove dropped items at old place
let dropped = $(this).parent().children().eq(oldIndex).remove()
// insert dropped items at new place
if (newIndex < oldIndex) {
target.before(dropped);
} else {
target.after(dropped);
}
}
function cancelDefault (e) {
e.preventDefault()
e.stopPropagation()
return false
}