【问题标题】:Scroll two divs with one srollbar (javascript)用一个滚动条滚动两个 div (javascript)
【发布时间】:2016-01-10 20:16:06
【问题描述】:

假设我们在一个页面上有两个 div,在 aspx 页面中预先创建。两者都应该水平滚动。但只有其中一个可以有滚动条,第二个 div 不应该有滚动条。

当您使用其滚动条滚动 div1 时,某些 javascript 函数应该平等地滚动 div2,即设置 div2 的滚动位置等于 div1 的滚动位置。它应该绝对同时完成,这样用户就不会看到两个 div 滚动之间有任何延迟。

怎么做?

【问题讨论】:

    标签: javascript asp.net scroll position


    【解决方案1】:

    /*jQuery(function ($) {
        $(".container").on("scroll", function () {
            $(".container").scrollTop($(this).scrollTop());
        });
    });*/
    
    
    var elems = document.getElementsByClassName("container");
    
    
    function foo() {
        var top = this.scrollTop;
    
        for (i = 0; i < elems.length; i++) {
            elems[i].scrollTop=top;
        }
    }
    
    for (i = 0; i < elems.length; i++) {
        elems[i].addEventListener("scroll", foo);
    }
    .container {
    
        height:400px;
    
        overflow-y:scroll;
    
        width:200px;
    
        background:Red;
    
        float:left;
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="parent">
        <div class="container">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
        <div class="container">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
    </div>

    试试这个:- http://jsfiddle.net/pLyrqxfj/

    JS:-

    jQuery(function ($) {
        $(".container").on("scroll", function () {
            $(".container").scrollTop($(this).scrollTop());
        });
    });
    

    HTML:-

    <div class="parent">
        <div class="container">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
        <div class="container">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
    </div>
    

    CSS:-

    .container {
    
        height:400px;
    
        overflow-y:scroll;
    
        width:200px;
    
        background:Red;
    
        float:left;
    
    }
    

    编辑:-

    纯 JavaScript:-

    var elems = document.getElementsByClassName("container");
    
    
    function foo() {
        var top = this.scrollTop;
    
        for (i = 0; i < elems.length; i++) {
            elems[i].scrollTop=top;
        }
    }
    
    for (i = 0; i < elems.length; i++) {
        elems[i].addEventListener("scroll", foo);
    }
    

    JSFiddle

    【讨论】:

    • @SWA 请参阅编辑部分。它使用纯 JavaScript。
    • @SWA 这能回答您的问题吗?
    • 是的,在某种程度上,我将其标记为有用的答案。但是您实际上通过引用它们的类来遍历所有 div,这在大多数情况下是不正确的。更多时候,您需要将某些 div 一起滚动,而不是全部滚动,并且还需要在某个方向滚动,并不总是垂直滚动,所以是的,您部分回答了。我有自己的答案,后来我想通了..
    • 可以使用左边栏的overflow-y: hidden来去掉中间的滚动条
    猜你喜欢
    • 2014-07-23
    • 2015-03-22
    • 2015-11-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多