HTML 5 Feature: Drag And Drop

In this article you will learn about drag and drop feature of HTML 5.

Earlier, we have used various third party libraries for implementing Drag and Drop functionality. So the problem of code management became difficult as code was not standard. But now with the introduction of HTML 5, a standard Drag and Drop API is introduced which helps us to achieve our functionality easily.

Today, I would like to demo the Drag and Drop functionality with small funny puzzle like page.

I will be creating face puzzle wherein attaching the parts of face like nose, eyes, and lips, create a face.

To implement Drag and Drop functionality, steps to be followed.

  1. Set the source element (element that you want to drag), draggable attribute to true.
  2. Handle onDrop event for the droppable container element.
  3. Handle onDragOver event because html container elements won’t allow anything to be dragged inside it.
  4. Execute & test it.

Let’s begin with our face puzzle implementation.

Step 1 - Create a HTML page

Create a HTML page with name DragDrop.html and copy the below css (Cascading Style Sheet) & html code.

HTML & Css Code

  1. <html>  
  2. <head>  
  3. <style>  
  4.         body {  
  5.             cursor: pointer;  
  6.             text-align: center;  
  7.         }  
  8.         .divdrag {  
  9.             position: relative;  
  10.             border: 0px solid rgba(0, 0, 0, .25);  
  11.             width: 300px;  
  12.             height: 300px;  
  13.             padding: 10px 10px10px10px;  
  14.             float: left;  
  15.         }  
  16.         .face {  
  17.             background-image: url('face.jpg');  
  18.             background-repeat: no-repeat;  
  19.             width: 424px;  
  20.             height: 510px;  
  21.             border: 1px dotted grey;  
  22.             padding: 0 0 0 0;  
  23.         }  
  24. .facetr td {  
  25.             text-align: center;  
  26.             border: 1px dotted #f7ecec;  
  27.         }  
  28. </style>  
  29. </head>  
  30. <body>  
  31.  <h2>Create the face</h2>  
  32.     <div class="divdrag">  
  33.     <img src="eye1.png" alt="eye" draggable="true" id="eye1" ondragstart="drag(event)" />  
  34.     <img src="eye2.png" alt="eye" draggable="true" id="eye2" ondragstart="drag(event)" />  
  35.     <img src="nose2.png" alt="nose" draggable="true" id="nose2" ondragstart="drag(event)" />  
  36.     <img src="eye4.png" alt="eye" draggable="true" id="eye4" ondragstart="drag(event)" />  
  37.     <img src="nose1.png" alt="nose" draggable="true" id="nose1" ondragstart="drag(event)" />  
  38.     <img src="eye3.png" alt="eye" draggable="true" id="eye3" ondragstart="drag(event)" />  
  39.     <img src="smile1.png" alt="smile" draggable="true" id="smile1" ondragstart="drag(event)" />  
  40.    <img src="smile3.png" alt="smile" draggable="true" id="smile2" ondragstart="drag(event)" />  
  41.     <img src="smile2.png" alt="smile" draggable="true" id="smile3" ondragstart="drag(event)" />  
  42.    </div>  
  43.    <div style="float:left;">  
  44.      <a href="DragDrop.html" title="Click here to reset" style="text-decoration:none;">  
  45.      <img src="direction.png" width="125" height="100" onclick="" />  
  46.      </a>  
  47.    </div>  
  48.     <div id="div1" style="width:300px;height:300px;float:left;">  
  49.       
  50. <table class="face">  
  51.           <tr>  
  52.                <td colspan="2" style="width:100%;"> </td>  
  53.            </tr>  
  54.             <tr>  
  55.                 <td colspan="2" style="width:100%;"> </td>  
  56.             </tr>  
  57.             <tr>  
  58.                 <td id="eye" style="width:50%" ondrop="drop(event)" ondragover="allowDrop(event)"></td>  
  59.                 <td id="eye" style="width:50%" ondrop="drop(event)" ondragover="allowDrop(event)"></td>  
  60.             </tr>  
  61.             <tr>  
  62.                 <td id="nose" ondrop="drop(event)" ondragover="allowDrop(event)" colspan="2"></td>  
  63.             </tr>  
  64.             <tr>  
  65.                 <td id="smile" ondrop="drop(event)" ondragover="allowDrop(event)" colspan="2"></td>  
  66.             </tr>  
  67.         </table>  
  68.     </div>  
  69. </body>  
  70. </html>  
Output:

output

Copy the JavaScript code as well.

JavaScript Code:
  1. function allowDrop(ev) {  
  2. ev.preventDefault();  
  3. }  
  4.   
  5. function drag(ev) {  
  6.   ev.dataTransfer.setData("text", ev.target.id);  
  7. }  
  8.   
  9. function drop(ev) {  
  10.   ev.preventDefault();  
  11.   var data = ev.dataTransfer.getData("text");  
  12.   if (data.indexOf(ev.target.id) == -1) {  
  13.     ev.dataTransfer.clearData();  
  14.   }  
  15.   else {  
  16.     ev.target.appendChild(document.getElementById(data));  
  17.   }  
  18. }
Explanation: 
  • I took all the image parts and dumped inside a div.

  • Each image is set with attribute draggable=true & ondragstart event is implemented which will call drag() function. This function will set the dragged image id using setData() method.

    ev.dataTransfer.setData("text", ev.target.id)

  • Take another div which is the face outline. This will act as a container to drop the dragged image. This div contains a table which specifies where you can drop the parts (Note: I had set the background image of table).

  • Where ever you wanted the dragged image to place, specify ondrop event on the destination element. I am calling drop() function. In this function you will read the dragged image using getData() method.

    var data = ev.dataTransfer.getData("text")

    Append the dragged image in the destination.

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

  • Now there is a small problem, you will still not be able to drop the face parts. The reason is, by default html container elements won’t allow anything to be dragged inside it. So to drop my fish, we must override the default behavior of that element. This could be done by calling ev.preventDefault() method. So I have attached onDragOver event to the destination element. I am calling allowDrop function.

  • For Reset, I am calling the same page again to keep it simple as shown below,
    1. <a href="DragDrop.html" title="Click here to reset" style="text-decoration:none;">  
    2. <img src="direction.png" width="125" height="100" onclick="" /></a>  

Step 2 - Execute & Test

Drag the images from the list and try dropping on the right side face. Click on arrow image if you wish to reset it.

output

Isn’t it simple and interesting? I hope you like it. Please share your comments and thoughts for this article whether it's good or bad. In the end, Sharing is valuable no matter what.