jQuery Sortable List - scroll bar jumps up when sorting

后端 未结 13 1128
慢半拍i
慢半拍i 2020-12-24 12:28

I created a demo: http://pastebin.me/584b9a86d715c9ba85b7bebf0375e237

When the scroll bar is at the bottom and you drag items to sort them, it causes the scroll bar

13条回答
  •  温柔的废话
    2020-12-24 13:23

    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);
        }
    });
    

提交回复
热议问题