Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS add class after scroll
let scrollpos = window.scrollY
const header = document.querySelector("nav")
const header_height = header.offsetHeight
const add_class = () => header.classList.add("fade-in")
const remove_class = () => header.classList.remove("fade-in")
window.addEventListener('scroll', function() {
scrollpos = window.scrollY;
if (scrollpos >= header_height) {
add_class()
} else {
remove_class()
}
console.log(scrollpos) // optional
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment