JQuery Sortable and automatic scrolling

后端 未结 9 2275
走了就别回头了
走了就别回头了 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:25

    Based on @marty 's answer, here is a fine tuned code that will: 1. Control speed of the scrolling 2. Will scroll down and scroll up without glitches. 3. Default speed is 7px at a time 4. movements of less than 7px will be ignored

    var previousLocation, previousDelta;
        $( ".selector" ).sortable({
            sort: function(event, ui) {
                var currentScrollTop = $(window).scrollTop(),
                    topHelper = ui.position.top,
                    delta = topHelper - currentScrollTop;
                setTimeout(function() {
                    if((delta < 7 && delta >=0) || (delta > -7 && delta <=0))
                        return;
                    if(delta > 7){
                        delta = 7;
                        if((topHelper - previousDelta) < previousLocation){
                            delta = (delta * -1);
                        }
                    }
                    if(delta < -7){
                        delta = -7;
                        if((topHelper - previousDelta) > previousLocation){
                            delta = (delta * -1);
                        }
                    }
    
                    $(window).scrollTop(currentScrollTop + delta);
                    previousLocation = topHelper; previousDelta = delta;
                }, 5);
            }
        });
    

提交回复
热议问题