Intersection Observer

Know when element is on the screen. Threshold accepts a value between 0 and 1 and it represents the percentage of the element that must be visible before isIntersecting becomes true.

const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    const intersecting = entry.isIntersecting
        if(intersecting) {
         // Do something
        } else {
         // Remove ?
        }
  })
}, { threshold: .25 })

observer.observe(document.getElementById("id"))