function isInViewport(element) { //判断方法
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
const myElement = document.querySelector('#data-board-sales-hopper'); //获取需要判断的dom
if (isInViewport(myElement)) {
alert('该元素在视口内')
} else {
alert('该元素不在视口内')
}
11-28
03-13
1279
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-12
2031
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-15
4103
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-26