我試圖製作一個可以通過拖放重新定位的元素列表。第一個要素Box 1在100%的時間內工作得很好。有時候第二個盒子可以工作,但其他的都不能按預期工作。當你開始拖動它們時,他們似乎立刻就開始了所有的拖曳事件。dragend,dragenter和dragleave當我拖動時立即觸發
如果有問題,我正在使用最新的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');
}
這裏的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>
這裏是我的代碼的jsfiddle:http://jsfiddle.net/zGSpP/5/
你使用jQuery的可拖動嗎?你在哪裏初始化元素爲可拖動的'element.draggable();'? – jbabey
這真的很奇怪。您應該使用JQueryUI小部件事件作爲解決方法:http://jqueryui.com/draggable/ – sdespont
jbabey:HTML5屬性'draggable = true' – Bill