2023-10-13 10:28:30 +00:00
|
|
|
import { DomHandler } from 'primevue/utils';
|
|
|
|
import BaseAnimate from './BaseAnimate';
|
|
|
|
|
|
|
|
const Animate = BaseAnimate.extend('animate', {
|
|
|
|
mounted(el, binding) {
|
|
|
|
el.setAttribute('data-pd-animate', true);
|
|
|
|
!this.isUnstyled() && DomHandler.addClass(el, 'p-animate');
|
|
|
|
|
|
|
|
this.bindIntersectionObserver(el, binding);
|
|
|
|
},
|
|
|
|
unmounted(el) {
|
|
|
|
this.unbindIntersectionObserver(el);
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
},
|
|
|
|
timeout: null,
|
|
|
|
observer: null,
|
|
|
|
methods: {
|
|
|
|
bindIntersectionObserver(el, binding) {
|
|
|
|
const options = {
|
|
|
|
root: null,
|
|
|
|
rootMargin: '0px',
|
|
|
|
threshold: 1.0
|
|
|
|
};
|
|
|
|
|
|
|
|
this.observer = new IntersectionObserver((element) => this.isVisible(element, el, binding), options);
|
|
|
|
this.observer.observe(el);
|
|
|
|
},
|
|
|
|
isVisible(target, el, binding) {
|
|
|
|
const [intersectionObserverEntry] = target;
|
|
|
|
|
|
|
|
intersectionObserverEntry.isIntersecting ? this.enter(el, binding) : this.leave(el, binding);
|
|
|
|
},
|
|
|
|
enter(el, binding) {
|
|
|
|
el.style.visibility = 'visible';
|
2023-10-17 12:32:18 +00:00
|
|
|
DomHandler.addMultipleClasses(el, binding.value.enterClass);
|
|
|
|
|
|
|
|
binding.modifiers.once && this.unbindIntersectionObserver(el);
|
2023-10-13 10:28:30 +00:00
|
|
|
},
|
|
|
|
leave(el, binding) {
|
|
|
|
DomHandler.removeClass(el, binding.value.enterClass);
|
|
|
|
|
|
|
|
if (binding.value.leaveClass) {
|
2023-10-17 12:32:18 +00:00
|
|
|
DomHandler.addMultipleClasses(el, binding.value.leaveClass);
|
2023-10-13 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const animationDuration = el.style.animationDuration || 500;
|
|
|
|
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
el.style.visibility = 'hidden';
|
|
|
|
}, animationDuration);
|
|
|
|
},
|
|
|
|
unbindIntersectionObserver(el) {
|
|
|
|
if (this.observer) {
|
|
|
|
this.observer.unobserve(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Animate;
|