簡體   English   中英

Intersection Observer 找出元素是顯示還是隱藏

[英]Intersection Observer find out element is shown or hidden

每當顯示或隱藏觀察到的元素時(達到閾值時),都會調用Intersection Observer回調函數。

那么我可以讓元素即將消失還是即將出現?

無需使用根邊距和閾值:

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

查看“根邊距”部分並使用那里的選項。 閾值也是您感興趣的選項。

如果我的理解是正確的,您應該查看topbottom屬性級別的entry.intersectionRect屬性,因為它考慮了與視口的交集矩形。

實際上,當top等於0 ,表示觀察到的條目從視口的底部出現,當bottom等於觀察到的條目高度時,表示它從頂部消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM