监听容器出现在视口

14 阅读1分钟
const listenAppear = (container) => {
  return new Promise((resove) => {
    if (window.IntersectionObserver) {
      const observer = new IntersectionObserver((entires) => {
        if (entires[0].intersectionRatio <= 0 ) return
        resove();
        observer.disconnect()
      }, {threshold: [0.25, 0.5, 0.75, 1]})
      observer.observe(container)
    }
  })
}


const appear = (container, cb) => {
  listenAppear(container).then(() => {
    cb()
  })
}



appear(document.getElementById('loadMore'), () => {
  console.log('出现在视口中')
})