下面是一个模态动画示例,类似于joshmorony 提出的第一个示例,在 ionic 4/5/6 中使用 angular。
src/app/animations/enter.ts
import { Animation, createAnimation } from '@ionic/angular';
export const modalEnterAnimation = (
baseEl: HTMLElement,
presentingEl?: HTMLElement,
): Animation => {
const backdropAnimation = createAnimation()
.addElement(baseEl.querySelector('ion-backdrop')!)
// .fromTo('opacity', 0.01, 'var(--backdrop-opacity)')
.beforeStyles({
'pointer-events': 'none'
})
.afterClearStyles(['pointer-events']);
const wrapperAnimation = createAnimation()
.addElement(baseEl.querySelectorAll('.modal-wrapper, .modal-shadow')!)
.beforeStyles({ 'opacity': 1 })
.keyframes([
{ offset: 0, transform: 'translateX(100vh)' },
{ offset: 1, transform: 'translateX(0vh)'},
]);
const baseAnimation = createAnimation()
.addElement(baseEl)
.easing('ease-out')
.duration(300)
.addAnimation([wrapperAnimation, backdropAnimation]);
return baseAnimation;
};
src/app/animations/leave.ts
import { Animation, createAnimation } from '@ionic/angular';
export const modalLeaveAnimation = (
baseEl: HTMLElement,
presentingEl?: HTMLElement,
): Animation => {
const backdropAnimation = createAnimation()
.addElement(baseEl.querySelector('ion-backdrop')!)
// .fromTo('opacity', 0.01, 'var(--backdrop-opacity)')
.beforeStyles({
'pointer-events': 'none'
})
.afterClearStyles(['pointer-events']);
const wrapperAnimation = createAnimation()
.addElement(baseEl.querySelectorAll('.modal-wrapper')!)
.beforeStyles({ 'opacity': 1 })
.keyframes([
{ offset: 0, transform: 'translateX(0vh)' },
{ offset: 1, transform: 'translateX(100vh)'},
]);
const baseAnimation = createAnimation()
.addElement(baseEl)
.easing('ease-out')
.duration(300)
.addAnimation([wrapperAnimation, backdropAnimation]);
return baseAnimation;
};