美文网首页
使用position:sticky实现吸顶效果

使用position:sticky实现吸顶效果

作者: 爱楚楚真是太好了 | 来源:发表于2019-12-04 15:19 被阅读0次
<div class="father">
    <div class="son">blue</div>
</div>
.father {
    overflow: visible;
    width: 100%;
    height: 100%;
    padding: 0 20px;
    border: 16px solid #f5f5f5;
    background-color: #fff;
}
.son {
    width: 100%;
    position: -webkit-sticky;
    position: sticky;
    top: 40px; 
    background-color: blue;
    z-index: 99;
    border-top: 20px solid #fff;
}

ps:使用sticky实现吸顶,需注意父元素overflow不能设为hidden(设置hidden则无法滚动),且还需考虑position:sticky;的兼容性。

相关文章

网友评论

      本文标题:使用position:sticky实现吸顶效果

      本文链接:https://www.haomeiwen.com/subject/clllgctx.html