Skip to content

Instantly share code, notes, and snippets.

@MomokoSanchez
Last active January 7, 2022 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MomokoSanchez/cf1fb20ccf6a035009876585dde4e307 to your computer and use it in GitHub Desktop.
Save MomokoSanchez/cf1fb20ccf6a035009876585dde4e307 to your computer and use it in GitHub Desktop.
Hide Navigation on scroll - jQuery
//Performance - caches a jQuery object containing the header element
var header = $(".yourHeaderClass");
// use prevScroll value to determine scroll direction
var prevScroll = $(window).scrollTop();
$(window).scroll(function() {
var scroll = $(window).scrollTop();
// Add class after scrolling 300px from top, remove class when scrolling up
// Remove (scroll > prevScroll) to hide nav both on scroll Up and scroll down
if ((scroll >= 300) && (scroll > prevScroll)) {
header.addClass("hideNav");
} else {
header.removeClass("hideNav");
}
prevScroll = scroll;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment