Skip to content

Latest commit

 

History

History
15 lines (13 loc) · 386 Bytes

check-if-an-element-is-in-the-viewport.md

File metadata and controls

15 lines (13 loc) · 386 Bytes

判断元素是否在视窗内

代码如下:

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