Skip to content

Instantly share code, notes, and snippets.

@nicoxxxcox
Last active May 2, 2022 19:48
Show Gist options
  • Save nicoxxxcox/e02ecc195b63f38f06e4a54c5bc1c4f1 to your computer and use it in GitHub Desktop.
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]
// 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