双向 "sticky" 位置("fixed" 时它是如何工作的)

bidirectional "sticky" position (how it is working when "fixed")

这是我的问题:

.parentSticky {
    width: 50%;
    height: 800px;
    border: solid black 5px;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: auto;
}
.siblingSticky {
    width: 100%;
    height: 100px;
    background-color: orange;
    border: solid 10px red;
    display: inline-block;
    flex: 50 0 1px;
    box-sizing: border-box;
}
.Istick {
    flex-grow: 1;
    border: solid 10px green;
    box-sizing: border-box;
    position: sticky;
    bottom: 10px;
    top: 10px;
}
<!-- break tags illustrate the page's other contents (scrolling demo) -->
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>



<div class="parentSticky">
    <div class="siblingSticky"> Element  </div>
    <div class="siblingSticky Istick" > "Sticky" element </div>
    <div class="siblingSticky"> Element </div>
</div>




<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

你忘记了粘性的一个重要部分,那就是:

A stickily positioned element is an element whose computed position value is sticky. It's treated as relatively positioned until its containing block crosses a specified threshold ref

所以在第二张图片中,粘性元素表现为相对元素。

简单来说,粘性元素只有在它因滚动而从屏幕上隐藏时才会表现为粘性,在这种情况下,粘性行为将强制保持可见.如果它已经可见(就像在你的第二张图片中),则不需要任何粘性行为并且该元素的行为就像它设置了 position:relative 一样。

top/bottom 仅用于定义偏移量。


相关问题了解更多详情:

Why element with position:sticky doesn't stick to the bottom of parent?