JQuery Sortable and automatic scrolling

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

    I think that you can consider handling scrolling external to sortable. I suggest to use timer and 'out' event of sortable.

    Here is piece of code based on jQueryUI demo page, you can use it as start point, if want to go this way:

    $(function() {
       var scroll = '';
       var $scrollable = $("#sortable");
       function scrolling(){
         if (scroll == 'up') {
           $scrollable.scrollTop($scrollable.scrollTop()-20);
           setTimeout(scrolling,50);
         }
         else if (scroll == 'down'){
           $scrollable.scrollTop($scrollable.scrollTop()+20);
           setTimeout(scrolling,50);
         }
       }
    
       $( "#sortable" ).sortable({
          scroll:false,
          out: function( event, ui ) {
            if (!ui.helper) return;
            if (ui.offset.top>0) {
              scroll='down';
            } else {
              scroll='up';
            }
            scrolling();
          },
          over: function( event, ui ) {
            scroll='';
          },
          deactivate:function( event, ui ) {
            scroll='';
          }
        });
        $( "#sortable").disableSelection(); 
    });
    

    Here is also working example: JSBIN

    sorry
    I did not lock example code and was destroyed incidentally. Now it's back to work.

提交回复
热议问题