2017-09-16 14 views

回答

0

您可以使用Intersection_Observer_API檢查時,該元素是在視口中並應用動畫。

https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

function handleIntersectionEntry(entry) { 
if (entry.length > 0) { 
    const item = entry[0]; 
    const ratio = Math.floor(item.intersectionRatio); 
    if (ratio > 0) { 
    // ITEM IN VIEWPORT 
    } else if (ratio < 1) { 
    // ITEM OUT OF VIEWPORT 
    } 
} 


    const config = { 
    root: null, 
    threshold: [1.0], 
    rootMargin: "0px" 
    }; 

    this.Observer = new IntersectionObserver(
    handleIntersectionEntry, 
    config 
); 

    this.Observer.observe('YOUR DOM ELEMENT TO MONITOR'); 

填充工具爲所有browswer支持:https://github.com/w3c/IntersectionObserver/tree/master/polyfill

+0

可否請您提供上述 – Parthcodes

+0

DEMO演示https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#A_simple_example –

+0

有沒有更簡單的方法來做到這一點? – Parthcodes