-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.list1.js
51 lines (43 loc) · 1.42 KB
/
script.list1.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
45
46
47
48
49
50
51
// script.js
function slist(target) {
//const theList = document.getElementById(target);
const items = document.getElementsByClassName("choice");
let current = null;
for (const item of items) {
item.draggable = true;
item.ondragstart = (e) => {
current = item;
for (const it of items) {
if (it !== current) {
it.classList.add("hint");
}
}
};
item.ondragenter = (e) => {
if (item !== current) {
item.classList.add("active");
}
};
item.ondragleave = () => item.classList.remove("active");
item.ondragend = () => {
for (const it of items) {
it.classList.remove("hint");
it.classList.remove("active");
}
};
item.ondragover = (e) => e.preventDefault();
item.ondrop = (e) => {
e.preventDefault();
if (item !== current) {
const currentPos = Array.from(items).indexOf(current);
const droppedPos = Array.from(items).indexOf(item);
if (currentPos < droppedPos) {
item.parentNode.insertBefore(current, item.nextSibling);
} else {
item.parentNode.insertBefore(current, item);
}
}
};
}
}
slist("sortlist");