Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dragend, dragenter, and dragleave firing off immediately when I drag

I'm trying to make a list of elements which can be repositioned by dragging and dropping. The first element, Box 1, works just fine 100% of the time. Sometimes the second box works, but none of the others work as expected. They seem to fire off all the drag events at once as soon as you start dragging them.

I'm using the latest Chrome (v23) if that matters.

var $questionItems = $('.question-item');  $questionItems   .on('dragstart', startDrag)   .on('dragend', removeDropSpaces)   .on('dragenter', toggleDropStyles)   .on('dragleave', toggleDropStyles);   function startDrag(){   console.log('dragging...');   addDropSpaces(); }  function toggleDropStyles(){   $(this).toggleClass('drag-over');   console.log(this); }   function addDropSpaces(){   $questionItems.after('<div class="empty-drop-target"></div>');   console.log('drop spaces added'); }  function removeDropSpaces(){   $('.empty-drop-target').remove();   console.log('drop spaces removed'); } 

Here's the HTML:

<div class="question-item" draggable="true">Box 1: Milk was a bad choice.</div> <div class="question-item" draggable="true">Box 2: I'm Ron Burgundy?</div> <div class="question-item" draggable="true">Box 3: You ate the entire wheel of cheese?</div> <div class="question-item" draggable="true">Box 4: Scotch scotch scotch</div> 

Here is a jsFiddle of my code: http://jsfiddle.net/zGSpP/5/

like image 788
Bill Avatar asked Jan 07 '13 20:01

Bill


2 Answers

Had this problem just now - it is a Chrome bug, you must not manipulate the DOM in dragStart event, or else dragEnd fires immediately. The solution for me turned out to be - use setTimeout() and manipulate the DOM inside the function you timeout.

like image 161
Ivan Koshelev Avatar answered Sep 20 '22 12:09

Ivan Koshelev


It seems to be a Chrome bug : https://groups.google.com/a/chromium.org/forum/?fromgroups=#!msg/chromium-bugs/YHs3orFC8Dc/ryT25b7J-NwJ

like image 40
sdespont Avatar answered Sep 19 '22 12:09

sdespont