jQuery UI, sortable,我能确定X位置的元素到Y了吗?

jQuery UI, sortable, can I determine that element from X position went to Y?

我这里只有这个例子:http://jqueryui.com/sortable/ 现在我想要一个 "magic" 事件告诉我,例如我将 item5 移动到 item2 它告诉我“5 moved到 2" 位置。

不,根据 api,我认为没有 "magic" 事件,但这是一种方法:

var startArray, stopArray;
$("#sortable").sortable({
    start: function(){ //save items in array before they are sorted
        startArray = $(this).sortable('toArray');
    },
    beforeStop: function(){ //save new sortorder of items
        stopArray = $(this).sortable('toArray');
    },
    update: function(){ //if the dom is changed, check the differences
        $.each(stopArray, function(index, value){
            if(startArray[index] !== value){ //if the values don't fit, they are changed
                console.log(value + ' is now on position ' + (index+1));
            }
        });

    }
});

Demo

参考

event-start

event-beforeStop

event-update

.each()