JQuery Sortable and automatic scrolling

后端 未结 9 2272
走了就别回头了
走了就别回头了 2021-02-05 10:02

I am trying to get JQuery Sortable to work but I have run into a slight usability problem.

The list that I am trying to sort is quite large (about 200 items). If the

相关标签:
9条回答
  • 2021-02-05 10:31

    While the value of the scrollSensitivity controls the scroll behaviour when the helper item approaches an edge (top or bottom), you can use the "sort" event to unconditionally scroll while you drag if you use the following code:

    $(".sortable").sortable({
        scroll: true,
        scrollSensitivity: 80,
        scrollSpeed: 3,
        sort: function(event, ui) {
            var currentScrollTop = $(window).scrollTop(),
                topHelper = ui.position.top,
                delta = topHelper - currentScrollTop;
            setTimeout(function() {
                $(window).scrollTop(currentScrollTop + delta);
            }, 5);
        }
    });
    

    Not sure if this completely addresses the issue you're seeing, but I found that usability with larger list improves with this approach. Here is a link to jsfiddle.

    0 讨论(0)
  • 2021-02-05 10:34

    I had a responsive table with bootstrap, this would not let it work.

    not like this:

    <div class="table-responsive">
       <table>
          ...
       </table>
    </div>
    

    like this yes:

     <table>
       ...
     </table>
    

    and use these options:

    scroll, scrollSensitivity, scrollSpeed

    0 讨论(0)
  • 2021-02-05 10:35

    Old topic but here is an example of scroll with a custom scrollable element

    var sortScrollInterval = null;
    var scrollDelta = 0;
    $('.selector').sortable({
        // [..]
        scroll: false,
        sort: function(event, ui) {
            var scrollContainer = $('#container');
            var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
    
            // Scroll top if cursor is into the first 20% of screen
            // Scroll bottom if cursor is into the last 20% of screen
            var top20 = vh * 0.2;
            var bottom20 = vh * 0.8;
            if ((ui.position.top <= top20) || (ui.position.top > bottom20)) {
                if (ui.position.top <= top20) {
                    // Closer to the top = quicker scroll
                    scrollDelta = -40 * ((top20 - ui.position.top) / top20);
                }
                else {
                    // Closer to the bottom = quicker scroll
                    scrollDelta = 40 * (1 - (ui.position.top - bottom20) / bottom20);
                }
    
                // Set interval
                if (null === sortScrollInterval) {
                    sortScrollInterval = setInterval(function() {
                        if (Math.abs(scrollDelta) > 10) {
                                $(scrollContainer).scrollTop($(scrollContainer).scrollTop() + scrollDelta);
                            }
                    }, 50);
                }
            }
            else if (null !== sortScrollInterval) {
                clearInterval(sortScrollInterval);
                sortScrollInterval = null;
            }
        },
        over: function(event, ui) {
            if (null !== sortScrollInterval) {
                clearInterval(sortScrollInterval);
                sortScrollInterval = null;
            }
        },
        deactivate: function(event, ui) {
            if (null !== sortScrollInterval) {
                clearInterval(sortScrollInterval);
                sortScrollInterval = null;
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题