dragend, dragenter i dragleave odpalają natychmiast po przeciągnięciu
Próbuję utworzyć listę elementów, które można zmienić, przeciągając i upuszczając. Pierwszy element, pole 1, działa w 100% dobrze. Czasami drugie pole działa, ale żadna z pozostałych nie działa zgodnie z oczekiwaniami. Wydaje się, że natychmiast odpalają wszystkie zdarzenia przeciągania, gdy tylko zaczniesz je przeciągać.
Jeśli to ma znaczenie, używam najnowszego Chrome (v23).
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');
}
Oto 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>
Oto jsFiddle mojego kodu:http://jsfiddle.net/zGSpP/5/