How to scroll two div elements at the same time

前端 未结 1 1546
遥遥无期
遥遥无期 2020-12-19 15:56

I want to scroll two div at the same time, and the divs are in a jquery ui dialog. I want to detect its scroll event, and then I could scroll them at the same time. but I fa

相关标签:
1条回答
  • 2020-12-19 16:29

    You need to attach the scroll event directly to the scrolled element and get the position values using: scrollTop(), scrollLeft(). Here is a quick example:

    JSnippet Demo

    $(function(){
        $("#dialog").dialog({
            width: 400,
            height: 400
        });
        $("#dialog div").on('scroll', function(e) { 
            var ele = $(e.currentTarget);
            var left = ele.scrollLeft();
            var top = ele.scrollTop();
            if (ele.attr("id") === 'div1') {
                $("#div2").scrollTop(top);
                $("#div2").scrollLeft(left);
            } else {
                $("#div1").scrollTop(top);
                $("#div1").scrollLeft(left);
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题