简体   繁体   English

如何仅使用容器div的滚动滚动两个div

[英]How to scroll for two divs with only the scroll of the container div

The first div 's content is longer, than the second div and I want to use one scrollbar (container div ) for both. 第一个div的内容比第二个div的内容长,我想同时使用一个滚动条(容器div )。

When scrolling down for the two div s, I need the second div to scroll until it reaches the end of its content, then stop scrolling and the first div continue scrolling until it reaches the end of its content. 向下滚动两个div ,我需要第二个div滚动直到其内容的结尾,然后停止滚动,而第一个div继续滚动直到其内容的结尾。

<div class="container">
  <div class="first"></div>
  <div class="second"></div>
</div>
<style>
  .container {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    overflow: auto;
    height: calc(100vh - 40rem);
  }

  .first {
    width: 50%;
  }

  .second {
    width: 50%;
  }
</style>

A possible solution could be using the code from this answer 一个可能的解决方案可能是使用此答案中的代码

var element= document.getElementById('firstelement');
element.scrollTop = countedPixel;

You can count the pixel scrolled like this or with the native js function Example here 你可以指望像滚动的像素或与本地js函数实例这里

<div onscroll="doSomething()">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM