Synchronized scrolling using jQuery?

后端 未结 8 1236
你的背包
你的背包 2020-11-28 09:29

I am trying to implement synchronized scrolling for two DIV with the following code.

DEMO

$(document).ready(function() {
    $(\"#div1\"         


        
8条回答
  •  情书的邮戳
    2020-11-28 09:46

    If the divs are of equal sizes then this code below is a simple way to scroll them synchronously:

    scroll_all_blocks: function(e) {
            var scrollLeft = $(e.target)[0].scrollLeft;
    
            var len = $('.scroll_class').length;
            for (var i = 0; i < len; i++)
            {
                $('.scroll_class')[i].scrollLeft = scrollLeft;
            }
    
        }
    

    Here im using horizontal scroll, but you can use scrollTop here instead. This function is call on scroll event on the div, so the e will have access to the event object. Secondly, you can simply have the ratio of corresponding sizes of the divs calculated to apply in this line $('.scroll_class')[i].scrollLeft = scrollLeft;

提交回复
热议问题