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