/**
* 判断元素是否出现在可视区域
* @param el{HTMLElement} html元素
* @param pre{number} 提前量,单位px
* @returns {boolean} 是否在可视区域
*/
function isVisible (el, pre = 0) {
// viewPortHeight 兼容所有浏览器写法
const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
const offsetTop = el.offsetTop
const scrollTop = document.documentElement.scrollTop
const top = offsetTop - scrollTop
// console.log('top', top)
// 这里有个+100是为了提前加载+ 100
return top <= viewPortHeight + pre
}
判断元素是否出现在可视区域
于 2023-03-27 10:24:37 首次发布