Intersection Observer template

PHOTO EMBED

Fri Dec 30 2022 04:33:48 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #intersection #observer

// intersection observer using a function
const ObserverTemplate = () => {

  const options = {
    rootMargin: '0px 0px -200px 0px',
    threshold: 0.5
  }

  const textObserver = new IntersectionObserver((entries) => {

      entries.forEach((entry) => {
            if(entry.isIntersecting){
              entry.target.classList.add('show')
            }else{
              entry.target.classList.remove('show')
            }
      })
  }, options)

  const h1Tag = getElement('h1')
  // console.log(h1Tag)

  textObserver.observe(h1Tag)
  
}



window.addEventListener('DOMContentLoaded', ObserverTemplate)
content_copyCOPY