[英]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.