Created
May 30, 2014 23:19
-
-
Save shawnr/6a2cffb06983bde95385 to your computer and use it in GitHub Desktop.
Show/Hide Header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Handle show/hide of header | |
// Technique borrowed from: https://medium.com/design-startups/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c | |
// Hide Header on on scroll down | |
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; | |
} | |
}, 250); | |
function hasScrolled() { | |
var st = $(window).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 | |
$('nav').removeClass('nav-down').addClass('nav-up'); | |
} else { | |
// Scroll Up | |
if(st + $(window).height() < $(document).height()) { | |
$('nav').removeClass('nav-up').addClass('nav-down'); | |
} | |
} | |
lastScrollTop = st; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment