gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:33:58 24 4
gpt4 key购买 nike

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

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

如何才能做到呢?

最佳答案

/*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

关于javascript - 用一个滚动条滚动两个 div (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com