HTML5 Drag and Drop
Drag and Drop is a very intuitive and powerful feature in HTML5. It allows users to select a draggable element with the mouse, "drag" it to a specified "drop zone," and "drop" it.
Making an Element Draggable
To make an element draggable, you need to set its draggable attribute to true.
By default, only links and images are draggable. For other elements like <div> or <p>, you must explicitly set draggable="true".
Drag and Drop Event Cycle
The entire drag and drop process consists of a series of events that we can listen to for controlling drag and drop behavior.
Events on the Dragged Element
ondragstart: Triggered when the user starts dragging an element. This is the starting point of the whole process.ondrag: Continuously triggered while the element is being dragged.ondragend: Triggered when the user completes dragging (whether successful drop or cancelled).
Events on the Drop Zone
ondragenter: Triggered when the dragged element enters the drop zone.ondragover: Continuously triggered while the dragged element moves over the drop zone.ondragleave: Triggered when the dragged element leaves the drop zone.ondrop: Triggered when the dragged element is released over the drop zone. This is the core of the drop operation.
A Complete Drag and Drop Example
Let's look at a complete example of dragging an image into a <div> container.
HTML:
CSS (for visual feedback):
JavaScript:
Code Analysis
allowDrop(event): This is the most easily overlooked but crucial step. By default, browsers prevent drop operations on elements. By callingevent.preventDefault(), we tell the browser: "This area is a valid drop target."drag(event): When dragging starts, we need a way to "remember" which element we're dragging.event.dataTransfer.setData("text", event.target.id)does exactly this. It stores the dragged element'sidas text in thedataTransferobject.drop(event): When the element is dropped, we first useevent.dataTransfer.getData("text")to retrieve the previously storedid, then usedocument.getElementById()to find this element, and finally useappendChild()to move it to the drop zone.