[英]How to toggle a sticky header?
The code below makes a sticky header which shows up once you've scrolled down 100px. 下面的代码创建了一个粘性标头,当您向下滚动100px时会显示该标头。 How do I make the sticky header turn off once I get to a lower part of the page?
进入页面下部后,如何关闭粘性页眉? Lets say 800px?
比方说800px?
Thanks! 谢谢!
$(window).scroll(function() {
if ($(this).scrollTop() > 100){
$('header').addClass("sticky");
}
else{
$('header').removeClass("sticky");
}
})
Do you mean like this? 你是这个意思吗
$(window).scroll(function() {
if ($(this).scrollTop() > 100 && $(this).scrollTop() < 800) {
$('header').addClass("sticky");
} else{
$('header').removeClass("sticky");
}
})
You could add another condition for when it the page gets to 800px: 您可以在页面达到800px时添加另一个条件:
$(window).scroll(function() {
if (($(this).scrollTop() > 100) && ($(this).scrollTop() < 800)){
$('header').addClass("sticky");
}
else{
$('header').removeClass("sticky");
}
});
In my example: 在我的示例中:
$(this).scrollTop()
is the variable representing the top of the view $(this).scrollTop()
是代表视图顶部的变量 if statement
adds the condition for the 100px addition if statement
的第一部分添加100px加法的条件 if statement
adds the condition for the 800px removal if statement
的第二部分添加了800px删除的条件 I would suggest only to use css. 我建议只使用CSS。 If the nav is sticky, why not keep it sticky at all times, and then add margin to the main-content-holder to make room for the nav.
如果导航栏是粘性的,为什么不始终保持粘性,然后在主要内容持有者中增加边距来为导航栏腾出空间。 For example: if the nav is 108px tall on desktop use:
例如:如果在桌面上使用的导航高度为108px,则:
.main-content-holder{
position:relative;
display:block;
margin-top:108px
}
then adjust height and/or position with media queries. 然后通过媒体查询调整高度和/或位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.