Lädt...

🔧 HTML5 Drag and Drop API 🌐


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

𖣠 HTML5 Drag and Drop API → A Step-by-Step Guide

The HTML5 Drag and Drop API lets you move elements around a webpage using your mouse. This feature is super useful for creating interactive user experiences like file uploads, kanban boards and more! 🖱️💡

🤷🏻‍♀️ What is Drag and Drop❓

Drag and Drop allows you to grab an element and move it somewhere else on the webpage. You can make any HTML element draggable using draggable="true".

🔥 Quick Summary

1️⃣ Make an item draggabledraggable="true" 🏗️

2️⃣ Detect when dragging startsdragstart 🎬

3️⃣ Allow droppingdragover

4️⃣ Move the itemdrop 🎯

✨ Step 1: Make an Item Draggable

First, you need an element that can be dragged. Add the draggable="true" attribute to make it draggable.

💁🏻‍♀️ Example:

<p id="dragItem" draggable="true">Drag me!</p>

👉 Now, this paragraph can be dragged! 🎉

🎬 Step 2: Detect When Dragging Starts

When you start dragging, you need to store some data about the dragged element. This is done using the dragstart event.

💁🏻‍♀️ Example:

document.getElementById("dragItem").addEventListener("dragstart", function(event) {
    event.dataTransfer.setData("text", event.target.id); // Store the ID of the dragged item
});

What happens here?

👉 When dragging starts, the element's ID is saved so we can use it later.

🚦 Step 3: Allow Dropping

By default, elements don't allow dropping. You must enable it using the dragover event.

💁🏻‍♀️ Example:

document.getElementById("dropArea").addEventListener("dragover", function(event) {
    event.preventDefault(); // This allows dropping
});

Why is this needed?

👉 Without event.preventDefault(), the item cannot be dropped inside another element.

➵ 𖦏 Step 4: Handle the Drop

Once the item is dropped, you need to move it inside the drop area.

💁🏻‍♀️ Example:

document.getElementById("dropArea").addEventListener("drop", function(event) {
    event.preventDefault();
    let draggedItem = event.dataTransfer.getData("text"); // Get the ID of dragged item
    event.target.appendChild(document.getElementById(draggedItem)); // Move it to new place
});

What happens here?

👉 Retrieves the ID of the dragged item and moves it inside the drop area.

🛠️ Step 5: Create a Drop Area

You need a container where the dragged item will be dropped.

💁🏻‍♀️ Example:

<div id="dropArea" style="width: 200px; height: 200px; border: 2px dashed black;">
    Drop Here
</div>

💡 Now, when you drag the item and drop it into this area, it will move inside!

🏆 Complete Example 👇

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Drag and Drop Example</title>
</head>
<body>

    <p id="dragItem" draggable="true">Drag me!</p>

    <div id="dropArea" style="width: 200px; height: 200px; border: 2px dashed black;">
        Drop Here
    </div>

    <script>
        // Step 2: Dragging starts
        document.getElementById("dragItem").addEventListener("dragstart", function(event) {
            event.dataTransfer.setData("text", event.target.id);
        });

        // Step 3: Allow dropping
        document.getElementById("dropArea").addEventListener("dragover", function(event) {
            event.preventDefault();
        });

        // Step 4: Handle the drop
        document.getElementById("dropArea").addEventListener("drop", function(event) {
            event.preventDefault();
            let draggedItem = event.dataTransfer.getData("text");
            event.target.appendChild(document.getElementById(draggedItem));
        });
    </script>

</body>
</html>

🧠 Understanding dataTransfer.setData()

The dataTransfer.setData(format, data) method is used to store data when dragging an item.

💁🏻‍♀️ Example:

event.dataTransfer.setData("text", event.target.id);

Explanation:

  • "text" → Data type (could be "text/plain", "text/html", etc.).
  • event.target.id → Saves the ID of the dragged item to be used later.

🛑 Why preventDefault() is Important?

By default, dropping is not allowed. We use event.preventDefault(); to enable it.

💁🏻‍♀️ Example:

document.getElementById("dropArea").addEventListener("dragover", function(event) {
    event.preventDefault(); // Allows dropping
});

⚡✪ Why do we need it?

  • In dragover → Enables dropping.
  • In drop → Prevents unwanted browser actions (like opening a file). 👉 Without preventDefault(), the item won’t be droppable.

✦ Full Flow Recap

📌 Drag startssetData saves the ID.

📌 Drag over drop areapreventDefault enables dropping.

📌 Drop happensgetData retrieves ID and moves item.

📢 Learn More & Connect with Me!

🤩 Want to see this in action?

Check out my Technical Presentation 🎥:

🔗 View Presentation

💼 Let’s connect on LinkedIn! 👥💬

Happy Coding!

...

🔧 HTML5 Drag and Drop API 🌐


📈 42.18 Punkte
🔧 Programmierung

🔧 Drag and Drop in HTML5


📈 37.52 Punkte
🔧 Programmierung

🍏 Flip HTML5 1.8.5 - Create Realistic Responsive HTML5 FlipBook from PDF.


📈 29.34 Punkte
🍏 iOS / Mac OS

📰 RuPaul’s Drag Race: Paramount+ holt die beliebte Drag-Queen-Show nach Deutschland


📈 26.13 Punkte
📰 IT Nachrichten

🔧 Vue Fluid DnD v0.7 Drag and drop across lists and more


📈 23.98 Punkte
🔧 Programmierung

🔧 Vue Fluid DnD v0.7 Drag and drop across lists and more


📈 23.98 Punkte
🔧 Programmierung

🔧 Implementing Drag and Drop to Arrange/Sort Items with React, Tailwind CSS, and Dnd-kit


📈 23.98 Punkte
🔧 Programmierung

🔧 Implementing Drag and Drop to Arrange/Sort Items with React, Tailwind CSS, and Dnd-kit


📈 23.98 Punkte
🔧 Programmierung

🔧 How I made a drag-and-drop file uploader with Vue3 and Firebase


📈 23.98 Punkte
🔧 Programmierung

🔧 Create a drag and drop with Tailwind CSS and JavaScript


📈 23.98 Punkte
🔧 Programmierung

🔧 How to create a drag and drop with Tailwind CSS and JavaScript


📈 23.98 Punkte
🔧 Programmierung

🪟 The new Outlook for Windows to get drag-and-drop support for downloading attachments and emails


📈 23.98 Punkte
🪟 Windows Tipps

🔧 Learn how to create a drag and drop with Tailwind CSS and JavaScript


📈 23.98 Punkte
🔧 Programmierung

🍏 Yoink 3.6.89 - Simplifies drag and drop between Spaces and fullscreen apps.


📈 23.98 Punkte
🍏 iOS / Mac OS

🔧 Drag and Drop Image uploader using React-dropzone and Cloudinary


📈 23.98 Punkte
🔧 Programmierung

🔧 Snapping into Place: Adding Precision and Boundaries to Your Drag-and-Drop Grid


📈 23.98 Punkte
🔧 Programmierung

🍏 Dropzone 4.5.5 - Open, transfer, and install files by drag-and-drop.


📈 23.98 Punkte
🍏 iOS / Mac OS

🔧 Drag and Drop Ui using html css and javascript https://www.instagram.com/webstreet_code/


📈 23.98 Punkte
🔧 Programmierung

🔧 Building a Responsive, Drag-and-Drop Image Gallery with React and Tailwind CSS 🎨


📈 23.98 Punkte
🔧 Programmierung

🪟 Drag and drop support rolls out between Outlook and Microsoft Teams


📈 23.98 Punkte
🪟 Windows Tipps

🔧 Building a Drag-and-Drop Kanban Board with React and dnd-kit


📈 23.98 Punkte
🔧 Programmierung

🔧 Building a Drag-and-Drop Kanban Board with React and dnd-kit


📈 23.98 Punkte
🔧 Programmierung

📰 Google App for iPhone and iPad Gets iMessage Integration, Drag and Drop Support


📈 23.98 Punkte
📰 IT Security Nachrichten

🔧 AI Tool: V0 with drag and drop and tailwindcss


📈 23.98 Punkte
🔧 Programmierung

🔧 Drag and Drop HTML elements and files


📈 23.98 Punkte
🔧 Programmierung

🔧 I made a live HTML drag-and-drop editor! check it out.


📈 22.85 Punkte
🔧 Programmierung

🔧 Curious case of Drag and Drop


📈 22.85 Punkte
🔧 Programmierung