Child

HTML5 Drag and Drop



The Drag and Drop Event is the most fabulous properties of HTML5. With the help of Drag and Drop features We can move an object from one place to another place. The all measure browsers support the dragging except of Safari 5.1.2. In the process of dragging an object we have to just grab an object from its original place and leave it or drag it where ever you want to drag it. All the process is done with the help of Java Script. The Drag and Drop Event may be a little bit complex but we have tried to make it very simple just take a look at below of the page for detail descriptions.

drag
Browser Support:
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the <video> element.

Here is the code:

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#div1 {width:350px;height:70px;padding:10px;border:1px solid green;}
#div2 {width:350px;height:70px;padding:10px;border:1px solid green;}
</style>
 
<script type="text/javascript">// <![CDATA[
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>
<center>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"><img id="drag1" src="http://www.webkittutorial.com/tutorial_images/html5_logo.png" alt="" width="100px" height="100px" draggable="true" ondragstart="drag(event)" id="drag1"/></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
</center>
</body>
</html>

Make an Element Draggable

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

<img draggable="true">

What to Drag - ondragstart and setData()

Then, specify what should happen when the element is dragged.

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

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

function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}

In this case, the data type is "Text" and 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 in other elements. To allow a drop, we must prevent the default handling of the element.

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

event.preventDefault()

Do the Drop - ondrop

When the dragged data is dropped, a 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 explained:

  • Call preventDefault() to prevent the browser default handling of the data (default is open as link on drop)
  • Get the dragged data with the dataTransfer.getData("Text") method. This method will return any data that was set to the same type in the setData() method
  • The dragged data is the id of the dragged element ("drag1")
  • Append the dragged element into the drop element