// adapted from http://jsfiddle.net/mariusc23/s6mLJ/31/ var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('#sticky').outerHeight(); $(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; // This is necessary so you never see what is "behind" the navbar. if (st > lastScrollTop && st > navbarHeight){ // Scroll Down $('#sticky').removeClass('header-show').addClass('header-hide'); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { $('#sticky').removeClass('header-hide').addClass('header-show'); } } lastScrollTop = st; }