jQuery UI sortable() - listitem jumps to top in Safari and Chrome

后端 未结 7 840
走了就别回头了
走了就别回头了 2020-12-17 09:23

I have a sortable unordered list on the bottom of my page, which works perfect in Firefox. However, in Safari/Chrome the grabbed listitem jumps instantly to the top of the p

7条回答
  •  臣服心动
    2020-12-17 10:00

    Seems like jQuery UI 1.9.2 solved the issue.

    If you are unable to change the library, there's a workaround including a simple scroll bar operation. The idea is simple:

    • Keep the previous scroll position every time you scroll.
    • Set scroll back to the previous position when you start dragging your element.

    Here you go;

    var lastScrollPosition = 0; //variables defined in upper scope
    var tempScrollPosition = 0;
    
    window.onscroll = function () { // Up to you requirement you may change it to another elemnet e.g $("#YourPanel").onscroll
        clearTimeout($.data(this, 'scrollTimer'));
        $.data(this, 'scrollTimer', setTimeout(function () {
            tempScrollPosition = lastScrollPosition; // Scrolls don't change from position a to b. They cover some numbers between a and b during the change to create a smooth sliding visual. That's why we pick the previous scroll position with a timer of 250ms.
        }, 250));
    
        lastScrollPosition = $(document).scrollTop(); // Up to you requirement you may change it to another elemnet e.g $("#YourPanel").onscroll
    };
    
    $("#YourSortablePanel").sortable({
        start: function (event, ui) {
            $(document).scrollTop(tempScrollPosition);
        }
    });
    

提交回复
热议问题