forked from Maykkkk/drag-and-drop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (61 loc) · 1.76 KB
/
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
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
const items = document.querySelectorAll('.item');
const container2 = document.getElementById('container2');
const successMessage = document.getElementById('successMessage');
let draggedItem = null;
// Add event listeners for drag events on the items
items.forEach(item => {
item.addEventListener('dragstart', dragStart);
item.addEventListener('dragend', dragEnd);
});
// Add event listeners for drop events on the containers
container2.addEventListener('dragover', dragOver);
container2.addEventListener('dragenter', dragEnter);
container2.addEventListener('dragleave', dragLeave);
container2.addEventListener('drop', drop);
// Drag Functions
function dragStart() {
draggedItem = this;
this.classList.add('dragging');
}
function dragEnd() {
draggedItem = null;
this.classList.remove('dragging');
}
// Drop Functions
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
this.classList.add('hovered');
}
function dragLeave() {
this.classList.remove('hovered');
}
function drop() {
this.classList.remove('hovered');
this.appendChild(draggedItem);
showSuccessMessage();
}
function showSuccessMessage() {
successMessage.style.display = 'block';
setTimeout(function () {
successMessage.style.display = 'none';
}, 2000);
}
function reset() {
container2.innerHTML = '';
successMessage.style.display = 'none';
items.forEach(item => {
document.getElementById('container1').appendChild(item);
});
}
function drop() {
this.classList.remove('hovered');
this.appendChild(draggedItem);
showSuccessMessage();
container2.classList.add('drop-animation');
setTimeout(function () {
container2.classList.remove('drop-animation');
}, 500);
}