Skip to content

Instantly share code, notes, and snippets.

@MomokoSanchez
Last active January 7, 2022 23:07
Show Gist options
  • Select an option

  • Save MomokoSanchez/5dcf8a4d10c691249a3a68b722374068 to your computer and use it in GitHub Desktop.

Select an option

Save MomokoSanchez/5dcf8a4d10c691249a3a68b722374068 to your computer and use it in GitHub Desktop.
var header = document.getElementsByClassName("yourHeaderClass")[0];
// Use prevScroll value to determine scroll direction
var prevScroll = window.pageYOffset;
window.onscroll = function() {
var scroll = window.pageYOffset;
// 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.classList.add("hideNav");
} else {
header.classList.remove("hideNav");
}
prevScroll = scroll;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment