Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 drag and drop

HTML5 Drag and Drop (Drag and Drop)


Drag and drop (Drag and drop) is part of the HTML5 standard.


data to the child component via RUNOOB.COM Drag the icon into the rectangle.


Drag and Drop

Drag and drop is a common feature, which means grabbing an object and dragging it to another position.

In HTML5, drag and drop is part of the standard, and any element can be dragged and dropped.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support dragging.

**Note:** Safari 5.1.2 does not support dragging.


HTML5 Drag and Drop Example

The following example is a simple drag-and-drop example:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

<style type="text/css">

#div1 {width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;}

style

<

script

function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); } function drop(ev) { ev.preventDefault(); var data=ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); }

script

head

<

body

<

p

Drag the RUNOOB.COM image into the rectangle:

p

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" div>
<br>
<img id="drag1" src="/images/logo.png" draggable="true" ondragstart="drag(event)" width="336" height="69" body html>

Try it »

It may look a bit complicated, but we can examine the different parts of the drag-and-drop events separately.


Setting an Element as Draggable

First, to make an element draggable, set the draggable attribute to true:


What to drag - ondragstart and setData()

Then, specify what happens when the element is dragged.

In the example above, the ondragstart attribute calls a function, drag(event), which specifies the data to be dragged.

The dataTransfer.setData() method sets the data type and value of the dragged data:

function drag(ev)

{

    ev.dataTransfer.setData("Text",ev.target.id);

}

Text is a DOMString representing the type of the drag data to be added to the drag object. The value is the id of the draggable element ("drag1").


Where to drop - ondragover

The ondragover event specifies where the dragged data can be dropped.

By default, data/elements cannot be dropped into other elements. If you need to allow dropping, we must prevent the default handling of the element.

This is done by calling the event.preventDefault() method of the ondragover event:

event

.preventDefault()


Performing the drop - ondrop

When the dragged data is dropped, the drop event occurs.

In the example above, the ondrop attribute calls a function, drop(event):

function drop(ev)

{

    ev.preventDefault();

    var data=ev.dataTransfer.getData("Text");

    ev.target.appendChild(document.getElementById(data));

}

Code Explanation:


More examples

Drag and Drop Images Back and Forth
How to drag and drop an image between two

elements.