# 判断元素是否在可视范围内
# 快速导航
# 实例代码
function elementIsVisibleInViewport(el, partiallyVisible = false) {
const {top, left, bottom, right} = el.getBoundingClientRect()
return partiallyVisible ?
((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) :
top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
提示
partiallyVisible为是否为完全可见
分享
留言
解答
收藏