const options = { rootMargin: '0px 0px -200px 0px' } const observer = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.isIntersecting) { entry.target.classList.add('show'); observer.unobserve(entry.target); } else { return; } }) }, options); const h1 = document.querySelector('h1'); // targets one element observer.observe(h1); const paras = document.querySelectorAll('p'); paras.forEach(p => observer.observe(p)); //targets multple element with a loop
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter