Is there a way to only show the navigation bar when a user scrolls to the very top of the page? Currently, the nav bar appears whenever the user scrolls up, regardless of the screen position.
Html
<header class="nav-down">
<div> navigation </div>
</header>
Javascript
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('#navigation-container').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Ensure 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('navigation-container').addClass('nav-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('header').removeClass('nav-up').addClass('nav-down');
}
}
lastScrollTop = st;
}
CSS
header {
height: 40px;
position: fixed;
top: 0;
transition: top 0.5s ease-in-out;
width: 100%;
z-index: 999;
padding-top: 50px;
}
.nav-up {
top: -70px;
}
Thank you!