gpt4 book ai didi

javascript - 用滚动条滚动其他滚动条

转载 作者:搜寻专家 更新时间:2023-10-31 23:07:37 24 4
gpt4 key购买 nike

我有 3 个带滚动条的 div。如果我在 div 1 中滚动,我想在相反方向滚动 div 2 和 3。滚动的距离应该是 div 1 的距离的一半。

这就是我现在拥有的(小部分,其余部分在 jsfiddle 中),适用于 1 个 div。

$("#textBox1").scroll(function () {
console.log("scroll 1");
var offset = $("#textBox1").scrollTop() - scrollPosTBox1;
var half_offset = offset/2.0;

disable1 = true;

if(disable2 == false) {
$("#textBox2").scrollTop(scrollPosTBox2 - half_offset);
}
if(disable3 == false) {
$("#textBox3").scrollTop(scrollPosTBox3 - half_offset);
}
disable1 = false;


});

但是,如果我尝试为其他 2 个 div 设置相同的内容,那么我将无法再滚动任何内容。这是因为例如 div 1 触发 div 2 而 div 2 触发回 div 1。我尝试使用禁用代码修复此问题,但无济于事。

有人可以帮助我吗?

http://jsfiddle.net/XmYh5/1/

最佳答案

不要不尊重@EmreErkan 和@Simon 的努力。这是一个无点击版本。

var $boxes = $("#textBox1,#textBox2,#textBox3"),
active;

$boxes.scrollTop(150);

// set initial scrollTop values
updateScrollPos();

// bind mouseenter:
// 1) find which panel is active
// 2) update scrollTop values

$boxes.mouseenter(function () {
active = this.id;
updateScrollPos();
});

// bind scroll for all boxes
$boxes.scroll(function (e) {

$this = $(this);

// check to see if we are dealing with the active box
// if true then set scrolltop of other boxes relative to the active box
if(this.id == active){

var $others = $boxes.not($this),
offset = $this.scrollTop()-$this.data("scroll"),
half_offset = offset / 2;

$others.each(function(){
$this = $(this);
$this.scrollTop($this.data("scroll") - half_offset);
});
}

});

// utility function:
// assign scrollTop values element's data attributes (data-scroll)

function updateScrollPos() {
$boxes.each(function(){
$this = $(this);
$this.data("scroll",$this.scrollTop());
});
}

Fiddle

关于javascript - 用滚动条滚动其他滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342894/

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