primevue-mirror/components/lib/animateonscroll/AnimateOnScroll.js

106 lines
4.0 KiB
JavaScript
Raw Normal View History

2023-10-13 10:28:30 +00:00
import { DomHandler } from 'primevue/utils';
import BaseAnimateOnScroll from './BaseAnimateOnScroll';
2023-10-13 10:28:30 +00:00
const AnimateOnScroll = BaseAnimateOnScroll.extend('animateonscroll', {
2023-10-26 00:54:11 +00:00
created() {
this.$value = this.$value || {};
this.$el.style.opacity = this.$value.enterClass ? '0' : '';
},
mounted() {
this.$el.setAttribute('data-pd-animateonscroll', true);
2023-10-13 10:28:30 +00:00
2023-10-26 00:54:11 +00:00
this.bindIntersectionObserver();
2023-10-13 10:28:30 +00:00
},
2023-10-26 00:54:11 +00:00
unmounted() {
this.unbindAnimationEvents();
this.unbindIntersectionObserver();
2023-10-13 10:28:30 +00:00
},
2023-10-26 00:54:11 +00:00
observer: undefined,
resetObserver: undefined,
isObserverActive: false,
animationState: undefined,
animationEndListener: undefined,
2023-10-13 10:28:30 +00:00
methods: {
2023-10-26 00:54:11 +00:00
bindAnimationEvents() {
if (!this.animationEndListener) {
this.animationEndListener = () => {
DomHandler.removeMultipleClasses(this.$el, [this.$value.enterClass, this.$value.leaveClass]);
!this.$modifiers.once && this.resetObserver.observe(this.$el);
this.unbindAnimationEvents();
};
2023-10-13 10:28:30 +00:00
2023-10-26 00:54:11 +00:00
this.$el.addEventListener('animationend', this.animationEndListener);
}
2023-10-13 10:28:30 +00:00
},
2023-10-26 00:54:11 +00:00
bindIntersectionObserver() {
const { root, rootMargin, threshold = 0.5 } = this.$value;
const options = { root, rootMargin, threshold };
// States
this.observer = new IntersectionObserver(([entry]) => {
if (this.isObserverActive) {
if (entry.boundingClientRect.top > 0) {
entry.isIntersecting ? this.enter() : this.leave();
}
} else if (entry.isIntersecting) {
this.$value.animateOnLoad ? this.enter() : (this.$el.style.opacity = '');
}
this.isObserverActive = true;
}, options);
setTimeout(() => this.observer.observe(this.$el), 0);
// Reset
this.resetObserver = new IntersectionObserver(
([entry]) => {
if (entry.boundingClientRect.top > 0 && !entry.isIntersecting) {
this.$el.style.opacity = this.$value.enterClass ? '0' : '';
DomHandler.removeMultipleClasses(this.$el, [this.$value.enterClass, this.$value.leaveClass]);
2023-10-17 12:32:18 +00:00
2023-10-26 00:54:11 +00:00
this.resetObserver.unobserve(this.$el);
}
this.animationState = undefined;
},
{ ...options, threshold: 0 }
);
2023-10-13 10:28:30 +00:00
},
2023-10-26 00:54:11 +00:00
enter() {
if (this.animationState !== 'enter' && this.$value.enterClass) {
this.$el.style.opacity = '';
DomHandler.removeMultipleClasses(this.$el, this.$value.leaveClass);
DomHandler.addMultipleClasses(this.$el, this.$value.enterClass);
2023-10-13 10:28:30 +00:00
2023-10-26 00:54:11 +00:00
this.$modifiers.once && this.unbindIntersectionObserver(this.$el);
2023-10-13 10:28:30 +00:00
2023-10-26 00:54:11 +00:00
this.bindAnimationEvents();
this.animationState = 'enter';
}
},
leave() {
if (this.animationState !== 'leave' && this.$value.leaveClass) {
this.$el.style.opacity = this.$value.enterClass ? '0' : '';
DomHandler.removeMultipleClasses(this.$el, this.$value.enterClass);
DomHandler.addMultipleClasses(this.$el, this.$value.leaveClass);
2023-10-13 10:28:30 +00:00
2023-10-26 00:54:11 +00:00
this.bindAnimationEvents();
this.animationState = 'leave';
}
2023-10-13 10:28:30 +00:00
},
2023-10-26 00:54:11 +00:00
unbindAnimationEvents() {
if (this.animationEndListener) {
this.$el.removeEventListener('animationend', this.animationEndListener);
this.animationEndListener = undefined;
2023-10-13 10:28:30 +00:00
}
2023-10-26 00:54:11 +00:00
},
unbindIntersectionObserver() {
this.observer?.unobserve(this.$el);
this.resetObserver?.unobserve(this.$el);
this.isObserverActive = false;
2023-10-13 10:28:30 +00:00
}
}
});
export default AnimateOnScroll;