Jquery sortable 'change' event element position

后端 未结 5 2033
生来不讨喜
生来不讨喜 2020-11-30 00:23

Is there way to get current position of helper being dragged on over new position ?

$(\"#sortable\").sortable({
        start: function (event, ui) {
                


        
5条回答
  •  自闭症患者
    2020-11-30 01:03

    UPDATED: 26/08/2016 to use the latest jquery and jquery ui version plus bootstrap to style it.

    • demo: http://so.lucafilosofi.com/jquery-sortable-change-event-element-position/
    $(function() {
        $('#sortable').sortable({
            start: function(event, ui) {
                var start_pos = ui.item.index();
                ui.item.data('start_pos', start_pos);
            },
            change: function(event, ui) {
                var start_pos = ui.item.data('start_pos');
                var index = ui.placeholder.index();
                if (start_pos < index) {
                    $('#sortable li:nth-child(' + index + ')').addClass('highlights');
                } else {
                    $('#sortable li:eq(' + (index + 1) + ')').addClass('highlights');
                }
            },
            update: function(event, ui) {
                $('#sortable li').removeClass('highlights');
            }
        });
    });
    

提交回复
热议问题