Last active
May 27, 2022 09:32
-
-
Save yankiara/6984c41af9e7b5463a1d169b9fb40114 to your computer and use it in GitHub Desktop.
Scroll direction for sticky header/footer, etc.
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
window.addEventListener( 'DOMContentLoaded', ()=> { | |
const body = document.body, | |
scrollUp = "scroll-up", | |
scrollDown = "scroll-down", | |
offset = 0; | |
let lastScroll = window.pageYOffset; | |
if ( lastScroll > offset ) { | |
body.classList.add(scrollUp); | |
} | |
window.addEventListener('scroll', ()=> { | |
const currentScroll = window.pageYOffset; | |
if ( currentScroll <= offset ) { | |
body.classList.remove(scrollUp); | |
return; | |
} | |
if ( currentScroll > lastScroll && !body.classList.contains(scrollDown) ) { | |
body.classList.remove(scrollUp); | |
body.classList.add(scrollDown); | |
} else if ( currentScroll < lastScroll && !body.classList.contains(scrollUp) ) { | |
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