gpt4 book ai didi

jquery - 改变开关位置行为

转载 作者:行者123 更新时间:2023-12-01 02:05:47 24 4
gpt4 key购买 nike

如何更改可排序中的开关行为?我的意思是默认有 1 - 2 - 3 - 4 http://jsfiddle.net/keGuN/当我交换 41 的位置时 - 它们会转换为 4 - 1 - 2 - 3。我只想交换 41(4 - 2 - 3 - 1) 的位置。怎么做?

最佳答案

我想我明白你想做什么。 jQuery 的 Sortable 并不是真正设计来完成您所要求的操作。但是,通过使用 Sortable 公开的事件,您可以将拖动的项目 (A) 与之前位于 A 放置位置 (B) 的项目交换位置。我认为这达到了你想要的效果:

jsFiddle demo

jQuery:

$(function() {
var oldIndex;

$("#sortable").sortable({
start: function(event, ui) {
oldIndex = ui.item.index();
},
stop: function(event, ui) {
var newIndex = ui.item.index();
var movingForward = newIndex > oldIndex;
var nextIndex = newIndex + (movingForward ? -1 : 1);

var items = $('#sortable > div');

// Find the element to move
var itemToMove = items.get(nextIndex);
if (itemToMove) {

// Find the element at the index where we want to move the itemToMove
var newLocation = $(items.get(oldIndex));

// Decide if it goes before or after
if (movingForward) {
$(itemToMove).insertBefore(newLocation);
} else {
$(itemToMove).insertAfter(newLocation);
}
}
}
});

$("#sortable").disableSelection();
});

唯一值得注意的问题是位置不会在拖动时更新,而是仅在将项目放置到新位置后更新。

关于jquery - 改变开关位置行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18747622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com