Qui una versione con scroll:
codice:var arr = document.querySelectorAll('.box'); addEventListener("scroll", () => { arr.forEach(el => { if (isScrolledIntoView(el) === true) { el.children[0].classList.add('in'); } else el.children[0].classList.remove('in'); }); }); function isScrolledIntoView(el) { var rect = el.getBoundingClientRect(); var elemTop = rect.top; var elemBottom = rect.bottom; var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight); return isVisible; }