Draggable List Template
An interactive list where users can reorder items with native drag-and-drop functionality.
This component is perfect for task managers, playlist builders, or any interface where users need to manually sort a list of items.

About this Template
This template provides a premium drag-and-drop experience using only the native HTML Drag and Drop API and vanilla JavaScript, requiring no external libraries. It works by setting the draggable="true"
attribute on list items and then attaching a series of event listeners to manage the drag-and-drop lifecycle. The CSS provides clear visual feedback to the user by styling the item as it's being dragged and highlighting the potential drop location.
Features
- Pure JavaScript & HTML: Uses the browser's built-in Drag and Drop API, so no external libraries are needed.
- Clear Visual Feedback: The item being dragged gets a special
.dragging
class, and potential drop targets get a.drag-over
class for clear styling hooks. - Intuitive Interaction: Users can grab an item by its handle and see exactly where it will be placed in the list upon release.
- Mobile & Touch Friendly: While this example focuses on mouse events, the foundational logic can be extended with touch event listeners (
touchstart
,touchmove
,touchend
) for mobile devices. - Self-Contained: The entire component is self-contained and ready to be used in any project.
Code Breakdown
Each list item (li
) has the HTML attribute draggable="true"
to enable the native browser behavior.
The JavaScript logic revolves around several key event listeners attached to each draggable item:
dragstart
: Fires when the user starts dragging an item. It adds a.dragging
class to the item and stores a reference to it.dragend
: Fires when the user stops dragging. It is used to clean up by removing the.dragging
class and clearing helper variables.dragover
: Fires continuously as an element is dragged over a valid drop target. We must callevent.preventDefault()
inside this handler to allow a drop to occur.dragenter
&dragleave
: These fire when the dragged item enters or leaves a potential drop target. They are used here to toggle a.drag-over
class for visual highlighting.drop
: Fires when the user releases the mouse over a valid drop target. This is where the core logic resides. The script determines where the dragged item should be placed relative to the drop target and usesinsertBefore()
orappendChild()
to move the original dragged element to its new position in the DOM.
Code
Here's the full code for the template: