【发布时间】:2018-07-12 09:19:40
【问题描述】:
使用jQuery droppable,我也有多个可拖动和可放置。一次只允许将一个可拖动对象拖放到一个元素上。但是,在放置之后,可拖动对象应该再次被赋予拖动能力。 我面临的问题是多个可拖动元素可以放在一个元素上。我的代码如下:
<div id="draggable_all" style="float: left; width: 300px;">
<div class="draggable">drag1111</div>
<div class="draggable">drag2222</div>
<div class="draggable">drag3333</div>
</div>
<div id="droppable_all" style="float: left; width:300px; ">
<div class="droppable"></div>
<div class="droppable"></div>
<div class="droppable"></div>
</div>
CSS:
<style type="text/css">
.droppable {
width: 120px;
height: 120px;
background: red;
margin-bottom: 30px;
/*position: absolute;*/
/*right: 0;*/
/*bottom: 0;*/
}
.draggable {
/*position: absolute;*/
z-index: 9;
/*top: 25px;*/
/*left: 20px;*/
width: 120px;
height: 120px;
background-color: green;
margin-bottom: 30px;
}
</style>
和 JS:
$(document).ready(function () {
$(".draggable").draggable({
revert: handleRevert
});
$(".draggable").data("orDraggable",$(".draggable").offset());
$(".droppable").droppable({
drop: handleDropEvent
//,
// out: function(event, ui){
// $(this).droppable('option', 'accept', '.drag-item');
// }
});
function handleRevert(e) {
if (e === false) {
$(this).data("uiDraggable").originalPosition = $(this).data("orDraggable");
return true
}
}
function handleDropEvent(event, ui) {
ui.draggable.position({
of: $(this),
my: 'left top',
at: 'left top'
});
//$(this).droppable('option', 'accept', ui.draggable);
// $(this).droppable('disable');
}
});
如何实现?
PS:SO 有类似的问题,但没有一个能很好地解决我的问题。例如,this question 试图解决它。但是这里的问题是:被丢弃的元素与它被丢弃的元素没有相同的对齐方式。
编辑: 我做了一个 jsfiddle here,对 CSS 做了一些改动。
【问题讨论】:
-
在你的 drop 中,禁用 droppable。然后您可以在拖动开始时启用它。
-
@Twisty,如何在拖动开始时启用它?任何代码?
-
@Twisty,我的
drop事件调用函数handleDrropEvent。所以我可以在函数末尾添加这一行:$(".droppable").droppable("disable");。我说的对吗? -
是否可以将可拖动对象仅拖放到可拖放对象中,或者其他任何地方?
-
@TJ,仅在 droppables 上
标签: jquery jquery-ui-droppable