// Scroll animations function onEntry(entry) { entry.forEach(change => { if (change.isIntersecting) { change.target.classList.add('element-show'); } }); } let options = { threshold: [0.5] }; let observer = new IntersectionObserver(onEntry, options); let elements = document.querySelectorAll('.element-animation'); for (let elm of elements) { observer.observe(elm); }
IF you want remove .element-show class after element leaving the viewport
document.addEventListener('DOMContentLoaded', () => { if (document.querySelector('.element-animation')) { function onEntry(entry) { entry.forEach(change => { if (change.isIntersecting) { change.target.classList.add("element-show"); } else { change.target.classList.remove("element-show"); } }); } let options = { threshold: [0.5] }; let observer = new IntersectionObserver(onEntry, options); let elements = document.querySelectorAll(".element-animation"); for (let elm of elements) { observer.observe(elm); } } });