Implementing this code allows for different classes to be applied to #nav based on whether the user is scrolling UP, DOWN, or at the top of the page.
.nav-down is applied when the user scrolls up
.nav-up is applied when the user scrolls down
.nav-down-top is applied when the user scrolls to the top of the page
The Solution Code
jQuery(document).ready(function($){
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('nav').outerHeight(true);
$(window).scroll(function(event) { didScroll = true; });
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 100);
function hasScrolled() {
if($( window ).width() > 768) {
var st = $(this).scrollTop();
if (Math.abs(lastScrollTop - st) <= delta)
return;
if (st > lastScrollTop) {
// Scroll Down
$('#s-nav').removeClass('nav-down').removeClass('nav-down-top').addClass('nav-up');
} else {
// Scroll Up (@ top of screen)
if (st === 0) {
$('#s-nav').removeClass('nav-up').removeClass('nav-down').addClass('nav-down-top');
} else { //if (st + $(window).height() < $(document).height()) {
$('#s-nav').removeClass('nav-up').removeClass('nav-down-top').addClass('nav-down');
}
}
}
lastScrollTop = st;
}
});
An issue arises where .nav-down-top doesn't always get applied when the user is at the top of the page or scrolls to the top without going far enough. The challenge lies in ensuring that .nav-down-top is consistently applied when the user reaches the top of the page.