Scroll 2 scrollbars with jquery the same time

后端 未结 4 1665
心在旅途
心在旅途 2020-12-09 04:59

Is it possible to scroll 2 scrollbars with one scrollbar?

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 05:36

    If you are experiencing slow scrolling when you are using your mousewheel on linking scrollbars with beeglebug solution, here is a trick to fix it.

    The cool thing is that it is kinda compact and it doesn't use any id, only classes, so much more sustainable.

    // First add the class "linked-scrollbar" to the elements you want to link
    
    // Then manually or dynamically add an attribute which will contain
    // the data if the element is currently scrolling or not
    $('.linked-scrollbar').attr("data-scrolling", "false");
    
    $('.linked-scrollbar').scroll(function(){
        if($(this).attr("data-scrolling") == "false"){
            $('.linked-scrollbar').not(this).attr("data-scrolling", "true");
            $('.linked-scrollbar').not(this).scrollLeft($(this).scrollLeft());
        }
        $(this).attr("data-scrolling", "false");
    });
    

    Here is a jsfiddle showing that bug and the fix.

提交回复
热议问题