Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 19, 2023 22:08
Show Gist options
  • Save danielhaim1/5731383 to your computer and use it in GitHub Desktop.
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.
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