You can use Intersection_Observer_API to check for when the element is in viewport and apply the animations.
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');
Polyfill for all browswer support: https://github.com/w3c/IntersectionObserver/tree/master/polyfill
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.