-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (38 loc) · 1.08 KB
/
script.js
File metadata and controls
46 lines (38 loc) · 1.08 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
const cards = document.querySelectorAll(".card");
const lists = document.querySelectorAll(".list");
// attach drag event listeners to cards
cards.forEach(card => {
card.addEventListener("dragstart", dragStart);
card.addEventListener("dragend", dragEnd);
});
// attach drop event listeners to lists
lists.forEach(list => {
list.addEventListener("dragover", dragOver);
list.addEventListener("dragenter", dragEnter);
list.addEventListener("dragleave", dragLeave);
list.addEventListener("drop", dragDrop);
});
// store dragged element ID for retrieval on drop
function dragStart(e) {
e.dataTransfer.setData("text/plain", this.id);
}
function dragEnd() {
console.log("Drag ended");
}
// prevent default to allow dropping
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
this.classList.add("over");
}
function dragLeave() {
this.classList.remove("over");
}
function dragDrop(e) {
const id = e.dataTransfer.getData("text/plain");
const card = document.getElementById(id);
this.appendChild(card);
this.classList.remove("over");
}