我了解你的目标,我已经更新了你的小提琴。从语义的角度来看,Reusable Draggable Widgets Panel 应该只是可拖动的小部件,而不是可排序的列表。这是有道理的,因为您通常不希望用户将已经使用/更改的小部件从他们自己的Target Widgets Sortable Lists 拖回主Reusable Draggable Widgets Panel,如新小提琴所示。作为旁注,Reusable Draggable Widgets Panel 确实采用了相同的助手:“克隆”选项,但需要额外注意,如此 SOpost 所示,确保显示原始克隆。但正如我之前所说,Reusable Draggable Widgets Panel 只是可拖动元素而不是排序列表更有意义。
这是更新后的fiddle。
快速概览和解释 (js):
function removeHighlight(){
// Enable all sortables
$('.sort').each(function(){
var $this = $(this);
$this.css('borderColor','gray');
$this.sortable('enable');
});
}
function addHighlight(){
// Check number of elements already in each sortable
$('.sort').not(this).each(function(){
var $this = $(this);
if($this.find('li:not(.l-header)').length >= 3){
$this.css('borderColor','red');
$this.sortable('disable');
} else {
$this.css('borderColor','gray');
$this.sortable('enable');
}
});
}
// use both mousedown and mouseup events to handle highlighting
// and enabling/disabling of sort lists.
// Using the stop/start callbacks of sortable/draggable ui plugin
// is unreliable, even after calling list.sortable("resfresh")
$('ul.sort, ul.d-widgets').on("mousedown", addHighlight);
$('ul.sort, ul.d-widgets').on("mouseup", removeHighlight);
$( ".d-widgets li:not(.l-header)" ).draggable({
connectToSortable: ".sort",
helper: "clone",
revert: "invalid"
});
$( "ul.disable-highlight li:not(.l-header)" ).disableSelection();
$('.sort').sortable({
revert: 'invalid',
connectWith: '.sort',
items: '> li:not(.l-header)'
});
html:
<ul class="disable-highlight d-widgets">
<li class='l-header'>Reusable Draggable Widgets Panel</li>
<li></li>
<li></li>
</ul>
<ul class='disable-highlight sort'>
<li class='l-header'>Target Widgets Sortable Lists</li>
<li></li>
<li></li>
</ul>
<ul class='disable-highlight sort'>
<li class='l-header'>Target Widgets Sortable Lists</li>
<li></li>
<li></li>
</ul>
<ul class='disable-highlight sort'>
<li class='l-header'>Target Widgets Sortable Lists</li>
<li></li>
<li></li>
</ul>
css:
.d-widgets, .sort { display:inline-block; margin:5px; border:1px solid grey; vertical-align:top; }
.d-widgets{
background-color: #81d8d0;
}
}
ul { min-width:60px; min-height:20px; }
li { width:50px; min-height:15px; margin:5px; border:1px solid silver; cursor:move; background-color:white; }
.l-header{
cursor:auto;
width:80px;
padding:4px;
border:none;
border-bottom:1px solid grey;
font-size:10px;
}
EDIT 用于销毁小部件:
正如我在评论中所说,从用户体验的角度来看,专门指定用于删除小部件的区域将是一个很好的方法,如我更新的 fiddle 所示。