我被这个问题困住了一会儿,我想我可以分享一下这个位置:sticky + flexbox gotcha:
我的粘性div工作良好,直到我切换到flexbox容器的视图,突然div不粘时,它被包装在一个flexbox父。
.flexbox-wrapper {
显示:flex;
身高:600 px;
}
.regular {
背景颜色:蓝色;
}
.sticky {
位置:-webkit-sticky;
位置:粘性;
上图:0;
背景颜色:红色;
}
< div class = " flexbox-wrapper”>
< div class = "常规" >
这是普通的盒子
< / div >
< div class = "粘" >
这是一个粘盒子
< / div >
< / div >
显示问题的JSFiddle
由于flex box元素默认为拉伸,因此所有元素的高度都相同,不能滚动。
添加align-self: flex-start到sticky元素,设置高度为auto,允许滚动,并修复它。
目前,所有主流浏览器都支持这个功能,但Safari仍然落后于-webkit-前缀,而除了Firefox之外的其他浏览器在位置方面存在一些问题:粘表。
.flexbox-wrapper {
display: flex;
overflow: auto;
height: 200px; /* Not necessary -- for example only */
}
.regular {
background-color: blue; /* Not necessary -- for example only */
height: 600px; /* Not necessary -- for example only */
}
.sticky {
position: -webkit-sticky; /* for Safari */
position: sticky;
top: 0;
align-self: flex-start; /* <-- this is the fix */
background-color: red; /* Not necessary -- for example only */
}
<div class="flexbox-wrapper">
<div class="regular">
This is the regular box
</div>
<div class="sticky">
This is the sticky box
</div>
</div>
显示解决方案的JSFiddle