我想出了这个解决方案:http://jsfiddle.net/FabienDemangeat/TBYWw/
这个想法是选择要移动的Li元素的索引和它的目标位置。
如果目标值小于要移动的li元素的索引,则效果将被反转。
有些部分不完美,但可以作为起点。我从"No Surprises"提供的片段中获得了灵感。
主要函数swapLiElements
交换两个li元素,并且回调函数作为参数允许轻松进行多次交换(请参见fiddle)。
function swapLiElements($northLi, $southLi, isPushingDown, duration, easing, callbackFunction) {
var movement = $northLi.outerHeight();
$northLi.css('position', 'relative');
$southLi.css('position', 'relative');
if(isPushingDown)
$northLi.css('z-index', '999');
else
$southLi.css('z-index', '999');
$northLi.animate({'top': movement}, {
duration: duration,
queue: false,
easing: easing,
complete: function() {
if(isPushingDown)
$northLi.insertAfter($southLi);
else
$southLi.insertBefore($northLi);
resetLiCssPosition($northLi);
resetLiCssPosition($southLi);
callbackFunction();
}
});
$southLi.animate({'top': -movement}, {
duration: duration,
queue: false,
easing: easing,
});
}
$(this).parent().append(this);
的意思是将当前元素移动到其父元素的最后一个位置。 - Aaron Hoffman$(this).parent().prepend(this);
- Joseph Woodward