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

后端 未结 7 825
走了就别回头了
走了就别回头了 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);
        }
    });
    
    0 讨论(0)
  • 2020-12-17 10:00

    If you have 'revert' option just remove it or set 'revert: false' in the sortable options. It works for me. Thanks.

    0 讨论(0)
  • 2020-12-17 10:01

    Here is the fix I found somewhere online... maybe even on SO. Works for me.

    elements.itemList.sortable({
        'start': function (event, ui) {
            //jQuery Ui-Sortable Overlay Offset Fix
            if ($.browser.webkit) {
                wscrolltop = $(window).scrollTop();
            }
        },
        'sort': function (event, ui) {
            //jQuery Ui-Sortable Overlay Offset Fix
            if ($.browser.webkit) {
                ui.helper.css({ 'top': ui.position.top + wscrolltop + 'px' });
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-17 10:02

    I had the same problem. It worked well in Firefox but kept jumping in Safari and Chrome. I finally fixed it by adding overflow: auto; to the unsorted list in my CSS.

    I didn't even have to specify the height of the UL.

    0 讨论(0)
  • 2020-12-17 10:14

    I had the exact same problem with sortable items jumping to the top in Chrome while the same code worked fine in Firefox. The issue was the containing element had no explicit height. Once I added a height to it, the item no longer jumped around.

    0 讨论(0)
  • 2020-12-17 10:21

    For me this was happening because there was a transition/animation set on the row position. Removing the transition fixed the problem.

    0 讨论(0)
提交回复
热议问题