Last active
May 2, 2022 19:48
-
-
Save nicoxxxcox/e02ecc195b63f38f06e4a54c5bc1c4f1 to your computer and use it in GitHub Desktop.
Javascript function to add an "active" class on a DOM element during scroll over [param]
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
// HTML tag to listen | |
const header = document.querySelector("[data-header]"); | |
const toggleActiveClassOnScroll = (elem) => { | |
window.addEventListener("scroll", function () { | |
window.scrollY >= 400 ? elem.classList.add("active") : elem.classList.remove("active"); | |
}) | |
} | |
toggleActiveClassOnScroll(header) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment