【发布时间】:2018-05-15 10:15:29
【问题描述】:
我们试图集成多个 drggable 和 droppable。在这种情况下,我们使用 sortable 来简化克隆功能。拖放后可拖动需要再次拖动。
我们如何将 sortable 限制为仅接收一个元素并在多个元素落入时恢复为原始元素。
在这种情况下,sortable 的过度功能看起来行为不端。
注释行代码用于禁用在可排序上删除第二个元素。这没有按预期工作。
启用我的注释代码时的两个问题:
- 从 droppable 移出后,可拖动克隆不会恢复到原始位置。
- draggable 元素从一个 droppable 移动到另一个可恢复到 draggable 的原始位置。
如需演示,请参阅 this jsfiddle
脚本:
// jQuery.noConflict();
jQuery( document ).ready(function() { init();});
function init() {
var mouse_button = false;
jQuery('.ui-draggable').live({
mousedown: function () {
mouse_button = true;
},
mouseup: function () {
if (jQuery(this).attr('data-pos') == 'out' && jQuery(this).attr('data-id')) {
var p = jQuery('#' + jQuery(this).attr('data-id'));
var offset = p.offset();
jQuery(this).hide();
jQuery(this).animate({ left: offset.left, top: offset.top, width: jQuery(this).width, height: jQuery(this).height }, 100, function () {
jQuery(this).remove();
$( ".ui-droppable" ).each(function() {
if($(this).children().length == 0) {
$( this ).removeClass("dontDrop");
}
});
//if(p[0].hasAttribute("draggable"))
p.draggable("enable");
// $('.ui-droppable').sortable('option', 'connectWith',$('.ui-droppable').not('.dontDrop'));
// $('.ui-draggable').draggable('option', 'connectToSortable',$('.ui-droppable').not('.dontDrop'));
});
}
mouse_button = false;
},
mouseout: function () {
if (mouse_button) {
mouse_button = false;
}
}
});
jQuery( '.ui-draggable' ).draggable( {
cursor: 'move',
helper: 'clone',
connectToSortable: ".ui-droppable",
revert: function (event, ui) {
}
} );
jQuery(".ui-droppable").sortable({
cursor: "move",
connectWith: ".ui-droppable",
receive: function (event, ui) {
if($(this).children().length >= 1) {
$(this).children().addClass('filled');
$(this).addClass('dontDrop');
$( ".ui-droppable" ).each(function() {
if($(this).children().length == 0) {
$( this ).removeClass("dontDrop");
}
});
// $('.ui-droppable').sortable('option', 'connectWith',$('.ui-droppable').not('.dontDrop'));
// $('.ui-draggable').draggable('option', 'connectToSortable',$('.ui-droppable').not('.dontDrop'));
}else {
$(this).children().removeClass('filled');
}
if (jQuery(this).data().sortable.currentItem) {
jQuery(this).data().sortable.currentItem.attr('data-id', jQuery(ui.item).attr("id"));
// if(jQuery(ui.item)[0].hasAttribute("draggable"))
jQuery(ui.item).draggable("disable");
}
},
out: function (event, ui) { if (ui.helper) { ui.helper.attr('data-pos', 'out'); } },
over: function (event, ui) { ui.helper.attr('data-pos', 'in'); }
});
}
【问题讨论】:
-
您输入了:“启用注释代码后三件事无法正常工作”,但该行后面的列表只有两个列表项 - 应该有第三个吗?
-
目前还不清楚您要完成什么。我不会使用
.droppable(),除了使用.sortable(),就像你在小提琴中一样。不清楚的是顺序和状态。如果我将“Test 1”拖到一个正方形上,究竟会发生什么? -
另外,在您的代码中,您从
jQuery()切换到$(),我认为这会给您带来问题。 -
用于代码清理和查看更简洁功能的初始通道。 jsfiddle.net/Twisty/7mmburcx/30 仍然不清楚您要达到的目标。
标签: javascript jquery jquery-ui drag-and-drop jquery-ui-sortable