Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorial for HTML5 drag&drop - sortable list

Do anyone know a really good tutorial for HTML5 drag&drop? Im making a toDo-list and I want to be able to reorder/sort it with this API. I've been googling for it like a mad man and now im start giving up... ANY tips is welcomed! Thanks!

p.s I really want to use html5 drag&drop API, not jQuery-sortable()

like image 487
tobbe Avatar asked May 14 '12 17:05

tobbe


People also ask

How do I drag data in HTML5?

Drag and Drop Process If you want to drag an element, you need to set the draggable attribute to true for that element. Set an event listener for dragstart that stores the data being dragged. The event listener dragstart will set the allowed effects (copy, move, link, or some combination).

How do I drag-and-drop an image in HTML 5?

Firstly, you need to mark the element (image in this case) as draggable which you want to drag. Now, you need to specify three attributes that will call the respective events or functions – ondragstart, ondragover, and ondrop. Finally, calling the respective JavaScript function will do the job.

What is drag in HTML?

HTML drag-and-drop uses the DOM event model and drag events inherited from mouse events . A typical drag operation begins when a user selects a draggable element, drags the element to a droppable element, and then releases the dragged element.


2 Answers

I've tried to keep this sample as simple as possible.

If you create a HTML list:

<ul>   <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Apples</li>   <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Oranges</li>   <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Bananas</li>   <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Strawberries</li> </ul> 

...and the following javascript:

var _el;  function dragOver(e) {   if (isBefore(_el, e.target))     e.target.parentNode.insertBefore(_el, e.target);   else     e.target.parentNode.insertBefore(_el, e.target.nextSibling); }  function dragStart(e) {   e.dataTransfer.effectAllowed = "move";   e.dataTransfer.setData("text/plain", null); // Thanks to bqlou for their comment.   _el = e.target; }  function isBefore(el1, el2) {   if (el2.parentNode === el1.parentNode)     for (var cur = el1.previousSibling; cur && cur.nodeType !== 9; cur = cur.previousSibling)       if (cur === el2)         return true;   return false; } 

... you should get a sortable list.

You can try the code on https://codepen.io/crouchingtigerhiddenadam/pen/qKXgap

Please be aware of the following bug in FireFox: https://developer.mozilla.org/en-US/docs/Web/Events/dragenter

Hope this helps.

like image 129
davidf Avatar answered Sep 23 '22 00:09

davidf


For a beginning to end tutorial, check this out: http://taximeeting.tumblr.com/post/26539340142/lightweight-jquery-plugin-for-html5-sortable-lists.

It's based on html5sortable: http://farhadi.ir/projects/html5sortable/. Another great tutorial on HTML5's drag and drop can be found here: http://www.html5rocks.com/en/tutorials/dnd/basics/.

like image 39
KendallB Avatar answered Sep 24 '22 00:09

KendallB