使用 Element.getBoundingClientRect() 方法获取元素的大小及其相对于视口的位置。

如果 ele 在视口中可见,则以下方法返回 true

const isInViewport = function (ele) {
  const rect = ele.getBoundingClientRect();
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  )
}