Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML 5 drag events

I am trying to build a reorderable list in JS and HTML. (trying to do it without using jQuery ui ) I can't seem to figure out why only the dragstart and dragend events fire when a list item is dragged. Anyone know why the other events not firing?

<ul>
    <li draggable="true" class="drag">1111111</li>
    <li draggable="true" class="drag">222222</li>
    <li draggable="true" class="drag">333333</li>
    <li draggable="true" class="drag">444444</li>
</ul>

<script type="text/javascript">
    var drags = document.querySelectorAll('.drag');
    [].forEach.call(drags, function(drag) {
      drag.addEventListener('dragstart', handleDragStart, false);
      drag.addEventListener('dragenter', handleDragEnter, false);
      drag.addEventListener('dragover', handleDragOver, false);
      drag.addEventListener('dragleave', handleDragLeave, false);
      drag.addEventListener('dragend', handleDragEnd, false);
    });

    function handleDragStart(e){
        console.log('handleDragStart');
    }

    function handleDragEnter(e){
        console.log('handleDragEnter');
    }

    function handleDragOver(e){
        console.log('handleDragOver');
    }

    function handleDragLeave(e){
        console.log('handleDragLeave');
    }

    function handleDragEnd(e){
        console.log('handleDragEnd');
    }
</script>
like image 493
jamie holliday Avatar asked Jul 17 '12 19:07

jamie holliday


People also ask

How do I drag data in HTML5?

getData() 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.

Does HTML5 support drag and drop?

Now HTML 5 came up with a Drag and Drop (DnD) API that brings native DnD support to the browser making it much easier to code up. HTML 5 DnD is supported by all the major browsers like Chrome, Firefox 3.5 and Safari 4 etc.

How do I drag and drop an image in HTML5?

Approach: We have given a rectangle area and an image, the task is to drag the image and drop it into the rectangle. We have to enable ondrop=”drop(event)” and ondragover=”allowDrop(event)” to make the rectangle for image insertion. The image link is inserted in the HTML page using <img> src attribute.


1 Answers

As others have mentioned it already works in Chrome. In Firefox you need to set dataTransfer on dragstart and you need to do an e.preventDefault() to make elements valid drop targets. After that everything starts working:

var drags = document.querySelectorAll('.drag');
[].forEach.call(drags, function(drag) {
  drag.addEventListener('dragstart', handleDragStart, false);
  drag.addEventListener('dragenter', handleDragEnter, false);
  drag.addEventListener('dragover', handleDragOver, false);
  drag.addEventListener('dragleave', handleDragLeave, false);
  drag.addEventListener('dragend', handleDragEnd, false);
  drag.addEventListener('drop', handleDragEnd, false);
});

function handleDragStart(e) {
  console.log('dragstart ' + e.target.innerText);
  e.dataTransfer.setData('text/plain', 'This text may be dragged')
}

function handleDragEnter(e) {
  console.log('dragenter ' + e.target.innerText);
  e.preventDefault();
}

function handleDragOver(e) {
  console.log('dragover ' + e.target.innerText);
  e.preventDefault();
}

function handleDragLeave(e) {
  console.log('dragleave ' + e.target.innerText);
}

function handleDragEnd(e) {
  console.log('dragend ' + e.target.innerText);
  e.preventDefault();
}
<div draggable="true" class="drag">AAAAAA</div>
<div draggable="true" class="drag">BBBBBB</div>
<div draggable="true" class="drag">CCCCCC</div>
<div draggable="true" class="drag">DDDDDD</div>
<div draggable="true" class="drag">EEEEEE</div>

Note: I also added a drop handler so that Firefox wouldn't try to load a URL when you drop something.

like image 133
robertc Avatar answered Sep 28 '22 08:09

robertc