nav {
position: sticky; /* tells the element to act 'fixed' within it's
parent container */
top: 0; /* sets the nav at the top of the container or highest point
of the container still on the screen */
}
.sticky-container {
position: relative; /* makes sure that this container is seen
as an anchor by the 'sticky' navbar */
}
1条答案
按热度按时间pgky5nke1#
我想你要找的是
position: sticky
,正如here所解释的。因此,是的,它可以用CSS来完成。
第一个
导航栏上的样式设置了
position: sticky
和top: 0
,这意味着滚动时它会粘在窗口的顶部,只要它的锚被呈现。锚点由它最近的父节点定义,其值为position: relative
。在本例中,它是div.sticky-container
。你可能想在HTML中摆弄一下如何构造你的部分,但是如果我理解正确的话,这应该可以。
希望这对你有帮助!