一旦掉落的物品数量达到一定限度,避免掉落
Avoid drop once that dropped items count reach some limit
我正在使用 Jquery UI 可拖动。
一旦将接收该项目的 div 达到限制,我试图避免项目被丢弃。正如您在下面看到的,这是我的代码。
$( "#grid-list" ).droppable({
over: function(event, ui) {
count = 0;
$("#grid-list").find("li").each(function () {
count++;
if(count > $("#grid-list").data("albumlist")){
alert("limit reached");
}
});
}
});
JS Fiddle 示例:http://jsfiddle.net/r1Lnxby9/
编辑、更新
尝试从 html
中删除 ondragstart="drag(event)"
和 draggable="true"
,drag
未在 js
中定义;在 .droppable()
选项中设置 tolerance:"touch"
;将 $("> li", this).length
替换为 count
, $("#grid-list").each()
; >=
for >
如果预期结果是 1
个元素在 .droppable()
个元素中的最大值;在 over
事件
中调用 event.preventDefault()
, event.stopPropagation()
$("#list-albuns, #grid-list").sortable({
connectWith: ".connectedSortable"
}).disableSelection();
$("#grid-list").droppable({
tolerance: "touch",
over: function(event, ui) {
if ($("> li", this).length >= $(this).data("albumlist")) {
event.preventDefault();
event.stopPropagation();
alert("limit");
}
}
});
ul li {
display: inline-block;
list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
</script>
<ul id="list-albuns" class="connectedSortable ui-sortable">
<li id="0" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="0">
<img id="dragimg0" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/51552793.png" alt="Appetite for Destruction">
</li>
<li id="2" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="2">
<img id="dragimg2" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/97176319.png" alt="Use Your Illusion II">
</li>
<li id="3" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="3">
<img id="dragimg3" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/89710469.png" alt="40 Seasons - The Best Of Skid Row">
</li>
<li id="5" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="5">
<img id="dragimg5" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/56005467.jpg" alt="Danger Danger">
</li>
<li id="6" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="6">
<img id="dragimg6" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/85595741.jpg" alt="Screw It [Italy Bonus Tracks]">
</li>
<li id="7" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="7">
<img id="dragimg7" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/90791541.jpg" alt="Detonator">
</li>
</ul>Drop Here:
<ul id="grid-list" data-albumlist="1" class="connectedSortable" style="width:500px; height:200px;display:block; border:2px solid black;"></ul>
jsfiddle http://jsfiddle.net/r1Lnxby9/2/
我正在使用 Jquery UI 可拖动。 一旦将接收该项目的 div 达到限制,我试图避免项目被丢弃。正如您在下面看到的,这是我的代码。
$( "#grid-list" ).droppable({
over: function(event, ui) {
count = 0;
$("#grid-list").find("li").each(function () {
count++;
if(count > $("#grid-list").data("albumlist")){
alert("limit reached");
}
});
}
});
JS Fiddle 示例:http://jsfiddle.net/r1Lnxby9/
编辑、更新
尝试从 html
中删除 ondragstart="drag(event)"
和 draggable="true"
,drag
未在 js
中定义;在 .droppable()
选项中设置 tolerance:"touch"
;将 $("> li", this).length
替换为 count
, $("#grid-list").each()
; >=
for >
如果预期结果是 1
个元素在 .droppable()
个元素中的最大值;在 over
事件
event.preventDefault()
, event.stopPropagation()
$("#list-albuns, #grid-list").sortable({
connectWith: ".connectedSortable"
}).disableSelection();
$("#grid-list").droppable({
tolerance: "touch",
over: function(event, ui) {
if ($("> li", this).length >= $(this).data("albumlist")) {
event.preventDefault();
event.stopPropagation();
alert("limit");
}
}
});
ul li {
display: inline-block;
list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
</script>
<ul id="list-albuns" class="connectedSortable ui-sortable">
<li id="0" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="0">
<img id="dragimg0" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/51552793.png" alt="Appetite for Destruction">
</li>
<li id="2" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="2">
<img id="dragimg2" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/97176319.png" alt="Use Your Illusion II">
</li>
<li id="3" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="3">
<img id="dragimg3" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/89710469.png" alt="40 Seasons - The Best Of Skid Row">
</li>
<li id="5" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="5">
<img id="dragimg5" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/56005467.jpg" alt="Danger Danger">
</li>
<li id="6" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="6">
<img id="dragimg6" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/85595741.jpg" alt="Screw It [Italy Bonus Tracks]">
</li>
<li id="7" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="7">
<img id="dragimg7" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/90791541.jpg" alt="Detonator">
</li>
</ul>Drop Here:
<ul id="grid-list" data-albumlist="1" class="connectedSortable" style="width:500px; height:200px;display:block; border:2px solid black;"></ul>
jsfiddle http://jsfiddle.net/r1Lnxby9/2/