[英]Javascript: Animate Div when it is in viewport
您可以使用Intersection_Observer_API來檢查元素何時在視口中並應用動畫。
https://developer.mozilla.org/zh-CN/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');
支持所有瀏覽器的Polyfill: https : //github.com/w3c/IntersectionObserver/tree/master/polyfill
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.