Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created February 5, 2021 16:06
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 aaronsummers/647a4600df7064f5645e58c79a5a6902 to your computer and use it in GitHub Desktop.
Save aaronsummers/647a4600df7064f5645e58c79a5a6902 to your computer and use it in GitHub Desktop.
page scroll js
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