const div = document.getElementById("refresh")
let isInView = false
document.addEventListener("scroll", e => {
const bounding = div.getBoundingClientRect()
if (bounding.top < bounding.height && bounding.top > 0) {
// if you want it to only fire once when it comes into view
if (isInView) return
isInView = true
console.log("In view!")
// do other stuff...
} else {
isInView = false
}
})
2条答案
按热度按时间bcs8qyzn1#
这似乎行得通,也许有更好的办法。
pengsaosao2#
使用相交观察者判断:
https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver如果您使用IntersectionObserver,则需要考虑浏览器兼容性。