Skip to content

Instantly share code, notes, and snippets.

@robspangler
Created April 4, 2016 15:15
Show Gist options
  • Save robspangler/be6ec60d9d4fdc8a3cd3e4d443087ed3 to your computer and use it in GitHub Desktop.
Save robspangler/be6ec60d9d4fdc8a3cd3e4d443087ed3 to your computer and use it in GitHub Desktop.
/* START: Nav functions
* Initially based on https://medium.com/@mariusc23/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c */
var didScroll;
var lastScrollTop = 0;
var delta = 25;
var navbarHeight = jQuery('header.main').outerHeight();
jQuery(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = jQuery(this).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 .up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight || st + jQuery(window).height() < jQuery(document).height()){
// Scroll Down
jQuery('header.main').addClass('compact');
}
lastScrollTop = st;
if (lastScrollTop < 108) {
jQuery('header.main').removeClass('compact');
}
/* end: Mod */
}
/* END: Hide Header on on scroll down */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment