forked from ujjwalnp/WT-old-question-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragging_dropping_xhtml.html
62 lines (46 loc) · 1.67 KB
/
dragging_dropping_xhtml.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="drag-item" draggable="true">
Drag Me!
</div>
<div id="drop-zone">
Drop Here!
</div>
<script>
const dragItem = document.getElementById("drag-item");
const dropZone = document.getElementById("drop-zone");
// Set event listeners for drag events on the drag item
dragItem.addEventListener("dragstart", dragStart);
dragItem.addEventListener("dragend", dragEnd);
// Set event listeners for drop events on the drop zone
dropZone.addEventListener("dragover", dragOver);
dropZone.addEventListener("drop", drop);
// Functions to handle drag events
function dragStart(event) {
// Set the data that will be transferred during the drag operation
event.dataTransfer.setData("text/plain", event.target.id);
}
function dragEnd(event) {
// Remove any styles applied to the drag item
event.target.style.opacity = "";
}
// Functions to handle drop events
function dragOver(event) {
// Prevent default behavior to allow drop
event.preventDefault();
}
function drop(event) {
// Get the id of the dragged item from the data transfer
const draggedItemId = event.dataTransfer.getData("text/plain");
// Get the dragged item element
const draggedItem = document.getElementById(draggedItemId);
// Move the dragged item to the drop zone
event.target.appendChild(draggedItem);
}
</script>
</body>
</html>