【发布时间】:2012-12-21 15:24:11
【问题描述】:
我正在尝试制作一个可以通过拖放重新定位的元素列表。第一个元素 Box 1 在 100% 的时间里都能正常工作。有时第二个盒子可以工作,但其他盒子都不能按预期工作。一旦您开始拖动它们,它们似乎会立即触发所有拖动事件。
如果重要的话,我正在使用最新的 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();将元素初始化为可拖动的? -
真的很奇怪。您应该使用 JQueryUI 小部件事件作为解决方法:jqueryui.com/draggable
-
jbabey:HTML5 属性 'draggable=true'
标签: javascript jquery html drag-and-drop draggable