【发布时间】:2023-03-27 17:36:05
【问题描述】:
我目前正在使用intersection observer 来检测元素何时离开视口。它是这样设置的:
const el = document.querySelector('#el')
const observer = new window.IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
console.log('LEAVE')
return
}
console.log('ENTER')
}, {
root: null,
threshold: 0,
})
observer.observe(el)
但我也想知道元素是在视口上方还是下方。有没有办法做到这一点?
【问题讨论】:
-
在控制台中检查后,entry 对象具有 a lot 的属性,例如
entry.target.offsetHeight... 不是解决方案,但是也许是线索
标签: javascript intersection-observer