Skip to content

Instantly share code, notes, and snippets.

@MZOG
Created September 25, 2019 08:16
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 MZOG/91ad0c57939b05eff14f4ca8008dc235 to your computer and use it in GitHub Desktop.
Save MZOG/91ad0c57939b05eff14f4ca8008dc235 to your computer and use it in GitHub Desktop.
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