HTML
<div class="draggable-items">
<div class="main-groups">Item 1</div>
<div class="main-groups">Item 2</div>
<div class="main-groups">Item 3</div>
<div class="main-groups">Item 4</div>
</div>
<div id="container"></div>
的jQuery
$(function() {
$( ".main-groups" ).draggable({
connectToSortable: "#container",
helper: "original",
revert: "invalid"
});
$( "#container" ).sortable({
revert: true,
receive: function(event, ui) {
var html = [];
$(this).find('.main-groups').each(function() {
html.push('<div class="main-groups"><b><a href="#'+$(this).attr("id")+'">'+$(this).html()+'</a></b></div>');
});
$(this).find('.main-groups').replaceWith(html.join(''));
}
});
});
答案 0 :(得分:1)
您可以更轻松地使用连接的可排序列表,如下所示:
<强> Working Example 强>
$(function () {
$(".draggable-items").sortable({
connectWith: "#container",
tolerance: "pointer",
over: function (event, ui) {
$('.ui-sortable-helper').outerWidth($(".draggable-items").innerWidth()).css('background', '#333');
}
});
$("#container").sortable({
connectWith: ".draggable-items",
tolerance: "pointer",
over: function (event, ui) {
$('.ui-sortable-helper').outerWidth($("#container").innerWidth()).css('background', 'blue');
}
});
});
答案 1 :(得分:0)
显示当悬停在容器上时,并且因为它设置为100%,它会更改宽度以适合容器。
要修复它,我将其修改为:
$( ".main-groups" ).draggable({
connectToSortable: "#accordion2",
helper: "original",
revert: "invalid",
start : function(event, ui) {
ui.helper.width($(this).width());
}
});
在悬停容器时,这不会让可拖动项目改变其宽度。它落在它上面时会改变它的宽度。
但我仍然没有设法弄清楚如何将容器中的掉落元素移除到原始占位符。