gpt4 book ai didi

html - 为什么不是位置 :sticky with left:0 working inside a scrollable container?

转载 作者:行者123 更新时间:2023-12-03 22:00:47 25 4
gpt4 key购买 nike

我有这个问题 - 我有一个包含一些行的容器,每行都有一个粘性左列,然后是多个其他列。现在在下面的示例中,这会起作用一段时间,但是当您滚动父容器时,位置粘性不再起作用,并且随着您滚动而被插入。

.container {
overflow-x: scroll;
}

.row {
display: flex;
width: 100%;

}

.item {
min-width: 173px;
}

.sticky {
min-width: 250px;
position: sticky;
left: 0;
background: red;
}
<div class="container">
<div class="row">
<div class="item sticky">STICKY ROW 1</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 2</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 3</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
</div>


有什么办法可以解决这个问题..还是有另一种方法来完成这个??

我想要的结果是能够将粘性 div 卡在左侧 0并让其他列在下方滚动..

现在我知道如果 overflow-x: scroll在实际 .row 上元素它按预期工作..但我可能有 100 多行所以滚动需要在父容器上

最佳答案

添加边框以更好地查看与使用块级元素和width:100% 的限制有关的问题。 (不是您正在设置的无用的设置,而是块元素的默认行为)

.container {
overflow-x: scroll;
}

.row {
display: flex;
border:1px solid;
}

.item {
min-width: 173px;
}

.sticky {
min-width: 250px;
position: sticky;
left: 0;
background: red;
}
<div class="container">
<div class="row">
<div class="item sticky">STICKY ROW 1</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 2</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 3</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
</div>


考虑使用 inline-flex 的内联级元素来解决问题。添加 min-width:100%确保您至少覆盖 100%如果内容很小,则为宽度。

.container {
overflow-x: scroll;
}

.row {
display: inline-flex;
min-width: 100%;
border:1px solid;
}

.item {
min-width: 173px;
}

.sticky {
min-width: 250px;
position: sticky;
left: 0;
background: red;
}
<div class="container">
<div class="row">
<div class="item sticky">STICKY ROW 1</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 2</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
<div class="row">
<div class="item sticky">STICKY ROW 3</div>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
</div>



一些相关问题:

Nested sticky element with zero left does not sticky

Why is my element not sticking to the left when using position sticky in css?

关于html - 为什么不是位置 :sticky with left:0 working inside a scrollable container?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619356/

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