JQuery Sortable and automatic scrolling

后端 未结 9 2246
走了就别回头了
走了就别回头了 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: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;
            }
        }
    });
    

提交回复
热议问题