Created
February 5, 2021 16:06
-
-
Save aaronsummers/647a4600df7064f5645e58c79a5a6902 to your computer and use it in GitHub Desktop.
page scroll js
This file contains 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 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; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment