Skip to content

Instantly share code, notes, and snippets.

@BulatSa
Created October 14, 2020 19:10
Show Gist options
  • Save BulatSa/bab9a610e1b119aa3e52f35014c980f0 to your computer and use it in GitHub Desktop.
Save BulatSa/bab9a610e1b119aa3e52f35014c980f0 to your computer and use it in GitHub Desktop.
Hide/show header on scroll
const body = document.body;
const nav = document.querySelector(".page-header nav");
const menu = document.querySelector(".page-header .menu");
const scrollUp = "scroll-up";
const scrollDown = "scroll-down";
let lastScroll = 0;
window.addEventListener("scroll", () => {
const currentScroll = window.pageYOffset;
if (currentScroll <= 0) {
body.classList.remove(scrollUp);
return;
}
if (currentScroll > lastScroll && !body.classList.contains(scrollDown)) {
// down
body.classList.remove(scrollUp);
body.classList.add(scrollDown);
} else if (currentScroll < lastScroll && body.classList.contains(scrollDown)) {
// up
body.classList.remove(scrollDown);
body.classList.add(scrollUp);
}
lastScroll = currentScroll;
});
// https://webdesign.tutsplus.com/tutorials/how-to-hide-reveal-a-sticky-header-on-scroll-with-javascript--cms-33756
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment