判断元素是否在视窗内
代码如下:
js
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)
)
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9