← Back toDrag and drop in React

An introduction to HTML 5 drag and drop

Written byPhuoc Nguyen
Created
25 Nov, 2023
Tags
HTML 5 drag and drop
In this series, we've been exploring how to make an element draggable by handling mouse events. By tracking the mouse's movement in both horizontal and vertical directions using the `mousedown`, `mousemove`, and `mouseup` events, we can update the element's position accordingly.
But did you know there's another way to add this functionality to your web page? HTML 5 drag and drop is a powerful feature that allows users to drag and drop elements on a web page. It's easy to use and can greatly enhance the user experience of your website.
With HTML 5 drag and drop, you can drag and drop files, images, text, and other elements between different parts of a web page. This feature is supported by all modern web browsers, making it a great choice for web developers who want to create dynamic and interactive web pages.

HTML 5 drag and drop vs mouse event handlers: Which one to use?

Previously, we made an element draggable by tracking mouse movement via mouse events. However, there are cases where we can't use this mechanism, such as dragging files from outside the browser.
Enter HTML 5 drag and drop – a built-in feature that allows developers to create draggable elements on their web pages. This feature offers several advantages over using mouse event handlers to make elements draggable.
For starters, HTML 5 drag and drop provides a standardized way to create drag and drop functionality across all modern web browsers. This means you don't have to worry about compatibility issues when implementing drag and drop on your web page.
Another advantage is that HTML 5 drag and drop allows you to drag elements from outside the browser window, such as files from your computer. This is not possible with mouse event handlers, which only track mouse movement within the browser window.
In addition, HTML 5 drag and drop provides a more intuitive user interface for dragging and dropping elements on a web page. Users can simply click on an element and drag it to its destination, without having to hold down any keys or buttons.
While mouse event handlers can still be used to create draggable elements, HTML 5 drag and drop provides a more robust and user-friendly solution. By taking advantage of this built-in feature, developers can create engaging and interactive web pages that provide users with a seamless drag and drop experience.

Making an element draggable in HTML 5

Implementing drag and drop functionality in HTML 5 is a relatively straightforward process. The first step is to identify the elements on your web page that you want to make draggable.
To make an element draggable, you need to set the `draggable` attribute to `true`. For example, if you have a `div` element that you want to make draggable, you would add the following code to the element:
html
<div draggable="true">
...
</div>
Take a look at the demo below:
If you drag the element and move it around the screen, you'll notice that the original element remains in the same position. Instead, the browser moves a cloned version of the element.
Don't worry about this issue for now. We'll cover how to customize the cloned element in our upcoming post.

HTML 5 drag and drop events

If you want to create drag and drop functionality in your web page, HTML 5 provides a set of events that can help you achieve this. Once you've set the `draggable` attribute, you'll need to define what happens when an element is dragged. This is where event listeners come into play.
There are two main categories of drag and drop events in HTML 5: drag events and drop events.

Drag events

Drag events are associated with the element being dragged. There are three main drag events:
  • `dragstart`: This event is triggered when the user starts dragging an element.
  • `drag`: This event is triggered repeatedly as the user drags the element.
  • `dragend`: This event is triggered when the user stops dragging the element.

Drop events

Drop events are associated with the drop target. There are four main drop events:
  • `dragenter`: This event is triggered when a draggable element enters a valid drop target.
  • `dragover`: This event is triggered repeatedly while a draggable element is being dragged over a valid drop target.
  • `dragleave`: This event is triggered when a draggable element leaves a valid drop target without being dropped.
  • `drop`: This event is triggered when a draggable element is dropped onto a valid drop target.
By using these different types of drag and drop events, you can create more interactive and engaging web pages. For example, you might use the `dragenter` and `dragleave` events to change the appearance of a drop target when an element is dragged over it, or use the `drop` event to upload files or perform other actions based on what has been dropped onto the page.
We will dive deeper into these events with real-life examples in our upcoming posts. So, stay tuned!

Conclusion

In this post, we've explored the benefits of using HTML 5 drag and drop to create draggable elements on a web page instead of relying on mouse event handlers. We've also learned how to make an element draggable in HTML 5 and discussed various drag and drop events that can be used to create more advanced functionality.
HTML 5 drag and drop is a powerful feature that can significantly improve the user experience on your website. By utilizing this built-in feature, developers can create captivating and interactive web pages that provide users with a seamless drag and drop experience.
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn