使用一个滚动条滚动两个div(javascript)

时间:2015-10-13 07:26:22

标签: javascript asp.net scroll position

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

当你用滚动条滚动div1时,一些javascript函数应该同等地滚动div2,即设置div2的滚动位置等于div1的滚动位置。它应该绝对同时完成,这样用户才能看到两个div之间的任何延迟滚动。

怎么做?

1 个答案:

答案 0 :(得分:2)



/*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);
}
&#13;
.container {

    height:400px;

    overflow-y:scroll;

    width:200px;

    background:Red;

    float:left;

}
&#13;
<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>
&#13;
&#13;
&#13;

试试这个: - 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