search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Enable Draggable Elements
Handle Drag Events on the Source
Make Drop Targets Accept Drops
Complete Working Snippet
Home Web Front-end H5 Tutorial How to Implement Drag and Drop in HTML5? (Code Snippet)

How to Implement Drag and Drop in HTML5? (Code Snippet)

Dec 30, 2025 am 02:02 AM

HTML5原生支持拖放,需设置draggable="true"启用元素拖拽,通过dragstart存数据、dragover调用preventDefault允许投放、drop读取数据实现完整流程。

How to Implement Drag and Drop in HTML5? (Code Snippet)

Drag and drop in HTML5 is built into the browser — no libraries needed. It relies on a set of events and proper attribute setup to work reliably.

Enable Draggable Elements

Add the draggable="true" attribute to any element you want users to be able to drag. By default, only images and links are draggable.

  • Text elements, divs, buttons — all need draggable="true"
  • Set draggable="false" to disable dragging on otherwise draggable elements (e.g., an image)
  • Styling feedback helps: use CSS like [draggable="true"] { cursor: move; }

Handle Drag Events on the Source

When dragging starts, the browser fires dragstart. Use it to store data with dataTransfer.setData().

  • Only text/plain, text/html, and text/uri-list are universally supported MIME types
  • Example: event.dataTransfer.setData("text/plain", "item-42");
  • You can also set a custom drag image using setDragImage() for better UX

Make Drop Targets Accept Drops

By default, most elements reject drops. Prevent the default behavior on dragover and drop to allow dropping.

  • Add event.preventDefault() inside dragover — this is required
  • In drop, read the data with dataTransfer.getData("text/plain")
  • Use dragenter or dragleave to highlight targets visually

Complete Working Snippet

Here’s a minimal but functional example:

Drag me

Drop here


<script><br> const item = document.getElementById("drag-item");<br> const zone = document.getElementById("drop-zone");<br><br> item.addEventListener("dragstart", e => {<br> e.dataTransfer.setData("text/plain", "moved-item");<br> });<br><br> zone.addEventListener("dragover", e => {<br> e.preventDefault(); // essential<br> });<br><br> zone.addEventListener("drop", e => {<br> e.preventDefault();<br> const data = e.dataTransfer.getData("text/plain");<br> zone.textContent = "Dropped: " data;<br> });<br> </script>

The above is the detailed content of How to Implement Drag and Drop in HTML5? (Code Snippet). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)