我在页面上安排了元素:
<div>
<div class="dragdrop" style="top:0px; left: 0px; ">1</div>
<div class="dragdrop" style="top:40px; left: 0px; ">2</div>
<div class="dragdrop" style="top:60px; left: 0px; ">3</div>
<div class="dragdrop" style="top:0px; left: 100px;">4</div>
<div class="dragdrop" style="top:40px; left: 100px;">5</div>
<div class="dragdrop" style="top:60px; left: 100px;">6</div>
</div>
如何使用jQuery UI(Draggable / Droppable)来实现如果一个div被丢弃到另一个div上,它们会交换位置? (如果它被拖到其他任何地方,它将恢复到原来的位置。)
感谢。
答案 0 :(得分:14)
以下是如何使用拖放http://jsfiddle.net/76yRN/1/
交换元素的示例关于在jquery jQuery draggable items lose their draggability after being swapped (with jsfiddle example)
中交换元素的另一个问题希望这有帮助
答案 1 :(得分:1)
您只需将元素从一个替换为另一个。前段时间我创建了一个演示,用于在UL列表之间交换元素。检查一下: http://www.authorcode.com/swap-elements-when-drag-one-onto-another-using-jquery-ui/
答案 2 :(得分:0)
我使用了一系列解决方案来实现这一目标,其中#large_tiles和#small_tiles是两个列表:
$(function() {
$("#large_tiles li, #small_tiles li").draggable({
zIndex: 2,
appendTo: "body",
});
initDroppable($("#large_tiles li, #small_tiles li"));
initSwap();
function initSwap() {
initDroppable($("#large_tiles li, #small_tiles li"));
initDraggable($("#large_tiles li, #small_tiles li"));
}
function initDraggable($elements) {
$elements.draggable({
zIndex: 2,
appendTo: "body",
helper: "clone",
start: function(e, ui) {
$(ui.helper).addClass("clone");
},
cursorAt: { left:50, top:75 }
});
}
function initDroppable($elements) {
$elements.droppable({
activeClass: "active-tile",
hoverClass: "hover-tile",
over: function(event, ui) {
var $this = $(this);
},
drop: function(event, ui) {
var $this = $(this);
var linew1 = $(this).after(ui.draggable.clone());
var linew2 = $(ui.draggable).after($(this).clone());
$(ui.draggable).remove();
$(this).remove();
initSwap();
}
});
}
});