Skip to content

Instantly share code, notes, and snippets.

@jamesmthornton
Created August 28, 2018 02:35
Show Gist options
  • Save jamesmthornton/26be5f8b7d3a15212a74e1667ebfdd07 to your computer and use it in GitHub Desktop.
Save jamesmthornton/26be5f8b7d3a15212a74e1667ebfdd07 to your computer and use it in GitHub Desktop.
Determine scroll direction and trigger a function depending on whether user is scrolling up or down
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if ( currentScroll < $(document).height() - $(window).height() ){
// If we're scrolling down the page or at the top of the page
if ( currentScroll > previousScroll || previousScroll < 300 || currentScroll < 300){
// hide nav
window.setTimeout(hideNav, 100);
// if scrolling up the page
} else if ( currentScroll < previousScroll ) {
//show the nav
window.setTimeout(showNav, 100);
} else {
// if we have other conditions, lets just hide the nav
window.setTimeout(hideNav, 100);
}
// reset these vars to be equal until scrolling again
previousScroll = currentScroll;
}
});
function hideNav() {
$("#fixed-header, #share-footer").removeClass("is-visible").addClass("is-hidden");
}
function showNav() {
$("#fixed-header, #share-footer").removeClass("is-hidden").addClass("is-visible");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment