我试图将条件语句添加到我的代码中,但似乎无法使其工作。我希望当用户位于屏幕顶部时将.nav-down-top
应用于#s-nav,而当不在页面顶部时,我将.nav-down
应用于#s-nav。
码
jQuery(document).ready(function($){
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('nav').outerHeight();
$(window).scroll(function(event) { didScroll = true; });
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 150);
function hasScrolled() {
if($( window ).width() > 768) {
var st = $(this).scrollTop();
if (Math.abs(lastScrollTop - st) <= delta)
return;
if (st > lastScrollTop && st > navbarHeight ) {
// Scroll Down
$('#s-nav').removeClass('nav-down').addClass('nav-up');
} else {
// !!!! Code issue
// Scroll Up (@ top of screen)
if ($(window).scrollTop() === 0) {
$('#s-nav').removeClass('nav-up').addClass('nav-down-top');
} else {
// Scroll Up (NOT @ top of screen)
if (st + $(window).height() < $(document).height()) {
$('#s-nav').removeClass('nav-up').addClass('nav-down');
}
//
} else {
$('#s-nav').removeClass('nav-up').addClass('nav-down');
}
lastScrollTop = st;
}
});
HTML
<nav id="s-nav" class="nav-down"> ...