gpt4 book ai didi

html - 如何防止绝对定位的孙子拉伸(stretch)容器 scrollHeight/scrollWidth?

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:25 25 4
gpt4 key购买 nike

我希望容器的滚动大小仅尊重它的子元素。

不幸的是,子元素(下例中的.scrollable)包含绝对定位的元素,这些元素可能会超出子元素的范围。我希望容器滚动大小忽略这些溢出。

我该怎么做?

.container {
width: 150px;
height: 150px;
border: 1px #ddd solid;
overflow: auto;
}

.scrollable {
width: 100px;
margin: 5px;
background: linear-gradient(#f79862, #fd6a02)
}

.row {
width: 100%;
position: relative;
height: 30px;
background: linear-gradient(#f79862, #fd6a02)
}

.overflow {
position: absolute;
top: 0;
left: 20px;
height: 400px;
width: 30px;
background: #09c;
z-index: 2;
}
<div class="container">
<div class="scrollable">
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
<div class="overflow">
</div>
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
</div>
</div>

在此示例的上下文中 - 我希望滚动在最后一条橙色线可见时立即结束,而不是显示所有蓝色条。

最佳答案

overflow:hidden设置为.scrollable div,使其不会超出其父div

试试这个

.container {
width: 150px;
height: 150px;
border: 1px #ddd solid;
overflow: auto;
}

.scrollable {
width: 100px;
margin: 5px;
background: linear-gradient(#f79862, #fd6a02);
overflow: hidden; /*added here*/
}

.row {
width: 100%;
position: relative;
height: 30px;
background: linear-gradient(#f79862, #fd6a02)
}

.overflow {
position: absolute;
top: 0;
left: 20px;
height: 400px;
width: 30px;
background: #09c;
z-index: 2;
}
<div class="container">
<div class="scrollable">
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
<div class="overflow">
</div>
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
</div>
</div>

关于html - 如何防止绝对定位的孙子拉伸(stretch)容器 scrollHeight/scrollWidth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53537698/

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