[英]Intersection Observer find out element is shown or hidden
無需使用根邊距和閾值:
const callback = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log("entering element");
} else {
console.log("leaving element");
}
});
};
https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
查看“根邊距”部分並使用那里的選項。 閾值也是您感興趣的選項。
如果我的理解是正確的,您應該查看top
和bottom
屬性級別的entry.intersectionRect
屬性,因為它考慮了與視口的交集矩形。
實際上,當top
等於0
,表示觀察到的條目從視口的底部出現,當bottom
等於觀察到的條目高度時,表示它從頂部消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.