无法使两个div一起滚动。

huangapple go评论59阅读模式
英文:

Can't make two divs scroll together

问题

我有一个包含两个可滚动块的父容器,其中一个是绝对定位的。这是因为我需要创建一个带有圆角的包装器。它们都保证具有相同的宽度。

<div class="container">
  <div class="head">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
  <div class="body">
    we like it, but a person who isn't familiar with his story may have some difficulty seeing how it would be so fitting, but it's hard to ignore the similarities between this dog and cat.
  </div>
</div>
.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
}

.head {
  overflow: auto;
  height: 40px;
}

.container .body {
  border: 2px solid #c4c;
  position: absolute;
  border-radius: 16px;
  right: 0;
  left: 0;
  bottom: 0;
  height: 50px;
  overflow: auto;
}

我尝试使用JavaScript 通过计算一个块的scrollLeft并将这个值分配给另一个块的scrollLeft来修复它。它可以工作,但会闪烁,并且感觉不像是一个合适的解决方案。是否有一种方法可以使它们像一个块一样同步滚动,而不需要JavaScript?

英文:

https://codepen.io/hatttfu/pen/WNYagMO

I have a parent container with two scrollable blocks inside, one of which is absolute positioned. It's because I need to make a wrapper with border-radiuses. They both guaranteed to have the same width.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
}

.head {
  overflow: auto;
  height: 40px;
}

.container .body {
  border: 2px solid #c4c;
  position: absolute;
  border-radius: 16px;
  right: 0;
  left: 0;
  bottom: 0;
  height: 50px;
  overflow: auto;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;head&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
  &lt;div class=&quot;body&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I tried to fix it with JS by calculating scrollLeft of one block and assigning this value to another's scrollLeft. It's working but flickering and it feels like it's not a proper solution. Is there a way to make both of them scroll synchronously as one block without JS?

答案1

得分: 0

以下是翻译好的部分:

  1. 一个想法可以是:

    • 在容器级别只有一个overflow
    • body部分中的包装器作为container::before元素进行管理
    • 包装器具有position: sticky
    • bodyhead都具有position: absolute以垂直放置它们
  2. 在容器和包装器上使用不同的背景颜色,只需使用z-index来使bodyz-index大于包装器的z-index

请注意,上述内容是有关CSS布局的说明和示例代码。

英文:

An idea can be :

  • to have only one overflow at container level
  • manage the wrapper in
    the body part as a container::before element
  • the wrapper have a position: sticky
  • the body and the head a position: absolute to place them vertically

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
  overflow: auto;
  position: relative;
}

.container::before {
  content: &#39; &#39;;
  border: 2px solid #c4c;
  position: sticky;
  border-radius: 16px;
  display: block;
  left: 0;
  top: 50%;
  bottom: 0;
  height: 50%;
  width: 100%;
  z-index: 2;
}

.body, .head {
  position: absolute;
}

.body {
  top: 15%;
}

.head {
  top: 65%;
  z-index: 3;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;head&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
  &lt;div class=&quot;body&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

With background different on container and wrapper just play with z-index to allow body to be greater than wrapper z-index

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  width: 400px;
  border: 1px solid #ccc;
  height: 100px;
  overflow: auto;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  white-space: nowrap;
  overflow: auto;
  position: relative;
  background: blue;
}

.container::before {
  content: &#39; &#39;;
  border: 2px solid #c4c;
  position: sticky;
  border-radius: 16px;
  display: block;
  left: 0;
  top: 50%;
  bottom: 0;
  height: 50%;
  width: 100%;
  background: yellow;
  z-index: 2;
}

.body, .head {
  position: absolute;
}

.body {
  top: 15%;
}

.head {
  top: 65%;
  z-index: 3;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;head&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
  &lt;div class=&quot;body&quot;&gt;
    we like it, but a person who isn&#39;t familiar with his story may have some difficulty seeing how it would be so fitting, but it&#39;s hard to ignore the similarities between this dog and cat.
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月27日 21:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定