import Vue from 'vue'
let allEl = []
Vue.directive('adshow', {
bind: (el, binding) => {
setTimeout(() => {
const elinfo = el.getBoundingClientRect()
allEl.push({
el: el,
el_top: elinfo.top + (elinfo.height / 2) + window.scrollY - window.innerHeight,
act: false
})
}, 10);
el.style.color = 'red'
// 滚动事件
const innerHeight = window.innerHeight
window.onscroll = function () {
const scrollY = window.scrollY
allEl.map((v, i) => {
if (v.el_top < scrollY && v.el_top > scrollY - innerHeight) {
if (!v.act) {
v.setT = setTimeout(() => {
console.log(`show${i}`)
}, 3000);
v.act = true
}
} else {
clearTimeout(v.setT)
v.act = false
}
})
}
},
update: function (el, binding) { }
})
判断元素是否在可视范围内
最新推荐文章于 2025-02-10 23:57:04 发布
