Created
October 14, 2020 19:10
-
-
Save BulatSa/bab9a610e1b119aa3e52f35014c980f0 to your computer and use it in GitHub Desktop.
Hide/show header on scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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