我有一个html标题(如下所述),其中包含一个主导航栏和一个侧栏。当用户向下滚动页面以使用下面的javascript隐藏顶部导航栏以添加css类“nav-up”时,我正在尝试向上滚动85px。我对此并没有任何喜悦,也无法弄清楚为什么他们的javascript不适用于滚动的规则,有谁知道我在这里做错了什么,我似乎无法弄明白这一点!?
基本HTML结构
<header id="masthead" class="site-header nav-down" role="banner">
<div id="navbar" class="navbar">
</div><!-- #navbar -->
<div id="sidebar">
</div><!-- #sidebar -->
</header><!-- #masthead -->
要申请的CSS
.nav-up {
top: -85px;
}
使用Javascript:
<script>
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = 85;
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
// Scroll Down
$('header').removeClass('nav-down').addClass('nav-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('header').removeClass('nav-up').addClass('nav-down');
}
}
lastScrollTop = st;
}
</script>