Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
53 views
in Technique[技术] by (71.8m points)

javascript - JS : 3 scrollbar synchronized

I need help please.
And thank you !

VERTICAL SCROLL

When ".right" scroll down = ".center" scroll top, ".left" scroll down / & reverse
When ".left" scroll down = ".center" scroll top, ".right" scroll down / & reverse
When ".center" scroll down = ".right" scroll down, ".left" scroll down / & reverse

Left & Center are ok but... not Right

HTML :

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>

<body>
<div class="contain-img">

        <div class="left">
<img src="img1">
<img src="img2">
<img src="img3">
            </div>

        <div class="center">
<img src="img1">
<img src="img2">
<img src="img3">
            </div>

        <div class="right">
<img src="img1">
<img src="img2">
<img src="img3">
            </div>
</div>
<script src="scroll.js"><script>
</body>

CSS :

.contain-img{
    display: flex;
    width: 100%;
    height: 50%;
    margin-left: auto;
    margin-right: auto;
}

.left, .center, .right{
    height: 300px; 
    width: 300px;
    margin: 0;
    overflow-y:scroll;
    border: 0;
    display: flex;
}

.left, .right{
    flex-direction: column;
}
.center{
    flex-direction: column-reverse;
}

.left img, .center img, .right img{
    display: block;
    height: 100%;
    width: 100%;
    
}

JS :

var timeout;

$('.left, .center, .right').on("scroll", function callback() {
    clearTimeout(timeout);

    var source = $(this),
        target = $(source.is(".left") ? '.center' : '.left');  
    target.off("scroll").scrollTop(-source.scrollTop());

    var source = $(this),
        target = $(source.is(".left") ? '.right' : '.left');  
    target.off("scroll").scrollTop(source.scrollTop());

    var source = $(this),
        target = $(source.is(".center") ? '.right' : '.center');  
    target.off("scroll").scrollTop(-source.scrollTop());

    var source = $(this),
        target = $(source.is(".center") ? '.left' : '.center');  
    target.off("scroll").scrollTop(-source.scrollTop());
    

    timeout = setTimeout(function() {
        target.on("scroll", callback);
    }, 100);
});```

 

question from:https://stackoverflow.com/questions/65894577/js-3-scrollbar-synchronized

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...