In this tutorial, we'll explore the HTML5 Drag and Drop API. The objective is to learn how to enable users to drag HTML elements and drop them into different areas on a webpage.
By the end of this tutorial, you'll be able to:
- Understand the fundamentals of the Drag and Drop API
- Implement drag and drop functionality on your webpages
Prerequisites
Basic understanding of HTML, CSS, and JavaScript is required to follow this tutorial.
The Drag and Drop API in HTML5 revolves around several events that are fired during the stages of dragging and dropping. Some of the key events include dragstart
, drag
, dragenter
, dragleave
, dragover
, drop
, and dragend
.
To implement drag and drop, we need to set the draggable
attribute to true
for the elements which can be dragged.
Example 1: Basic Drag and Drop
<div id="draggable" draggable="true">Drag me!</div>
<div id="droppable">Drop here</div>
<script>
var draggable = document.getElementById('draggable');
draggable.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', 'draggable');
});
var droppable = document.getElementById('droppable');
droppable.addEventListener('dragover', function(e) {
e.preventDefault();
});
droppable.addEventListener('drop', function(e) {
e.preventDefault();
var data = e.dataTransfer.getData('text');
if (data == 'draggable') {
droppable.appendChild(draggable);
}
});
</script>
In this example, we first make an element draggable by setting its draggable
attribute to true
. We then set up event listeners for the dragstart
, dragover
, and drop
events. When the drag starts, we store some data ('draggable'
) which we then use to check if the dropped item is the item we expect.
Example 2: Drag and Drop with Multiple Items
<div id="draggable1" draggable="true">Drag me 1!</div>
<div id="draggable2" draggable="true">Drag me 2!</div>
<div id="droppable">Drop here</div>
<script>
var draggable1 = document.getElementById('draggable1');
var draggable2 = document.getElementById('draggable2');
draggable1.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', 'draggable1');
});
draggable2.addEventListener('dragstart', function(e) {
e.dataTransfer.setData('text/plain', 'draggable2');
});
var droppable = document.getElementById('droppable');
droppable.addEventListener('dragover', function(e) {
e.preventDefault();
});
droppable.addEventListener('drop', function(e) {
e.preventDefault();
var data = e.dataTransfer.getData('text');
var element = document.getElementById(data);
if (element) {
droppable.appendChild(element);
}
});
</script>
This example is similar to the first one, but it shows how to handle multiple draggable elements. We store the id of the element being dragged and then use that id to append the element to the drop zone.
In this tutorial, you've learned how to use the HTML5 Drag and Drop API. You've seen how to make elements draggable, create a drop zone, and handle the data transfer during a drag and drop operation.
To further your learning, you could explore how to add visual feedback during drag and drop, like changing the cursor or highlighting the drop zone.
Solutions and tips can be found in the W3Schools Drag and Drop tutorial and Mozilla Developer Network's Drag and Drop guide.