英文:
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 -->
<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>
<!-- 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
以下是翻译好的部分:
-
一个想法可以是:
- 在容器级别只有一个
overflow
- 将
body
部分中的包装器作为container::before
元素进行管理 - 包装器具有
position: sticky
body
和head
都具有position: absolute
以垂直放置它们
- 在容器级别只有一个
-
在容器和包装器上使用不同的背景颜色,只需使用
z-index
来使body
的z-index
大于包装器的z-index
请注意,上述内容是有关CSS布局的说明和示例代码。
英文:
An idea can be :
- to have only one
overflow
at container level - manage the wrapper in
the body part as acontainer::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: ' ';
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 -->
<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>
<!-- 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: ' ';
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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论