Last active
March 19, 2023 22:08
-
-
Save danielhaim1/5731383 to your computer and use it in GitHub Desktop.
The JavaScript function adds a scroll event listener to the window object, which checks if the user has scrolled down to a specific element with the ID "ends". If the user has scrolled down to the element, it displays elements with the class "myClass", otherwise it hides them and adds the classes "energize" and "fadeIn" to their class list.
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
window.addEventListener('DOMContentLoaded', () => { | |
const ends = document.querySelectorAll('#ends'); | |
if (ends.length) { | |
window.addEventListener('scroll', () => { | |
const t = ends[0].getBoundingClientRect().top - window.innerHeight; | |
if (window.pageYOffset > t) { | |
const myClass = document.querySelectorAll('.myClass'); | |
myClass.forEach(elem => elem.style.display = 'block'); | |
} else { | |
const myClass = document.querySelectorAll('.myClass'); | |
myClass.forEach(elem => { | |
elem.style.display = 'none'; | |
elem.classList.add('energize', 'fadeIn'); | |
}); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment