Ionic modal comes with the standard animation of slide-in-up
. Is it possible that we can change the animation to fade-in
?
问题:
回答1:
In order to add custom transitions for Ionic Modal We will be using Ionic Modal Options enterAnimation
and leaveAnimationfrom
ModalOptions interface. For a modal there are transition states: On Enter of modal and and On Leave of modal when we close it. If you look at the Ionic Modal options interface you will find 2 options to add animations for both the states.
export interface ModalOptions {
showBackdrop?: boolean;
enableBackdropDismiss?: boolean;
enterAnimation?: string;
leaveAnimation?: string;
cssClass?: string;
}
We will use these options in modal to specify transition class we create using Animation class from ionic-angular
. So lets see how we can create and custom animations step by step.
Create 2 transition classes for enter and leave:
on-enter-translate.transition.ts
import { Animation, PageTransition } from 'ionic-angular';
export class ModalTranslateEnterTransition extends PageTransition {
public init() {
const ele = this.enteringView.pageRef().nativeElement;
const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
wrapper.beforeStyles({ 'transform': 'translateX(100%);', 'opacity': 1 });
wrapper.fromTo('transform', 'translateX(100%)', 'translateX(0)');
wrapper.fromTo('opacity', 1, 1);
this
.element(this.enteringView.pageRef())
.duration(500)
.easing('cubic-bezier(.1, .7, .1, 1)')
.add(wrapper);
}
}
on-leave-translate.transition.ts
import { Animation, PageTransition } from 'ionic-angular';
export class ModalTranslateLeaveTransition extends PageTransition {
public init() {
const ele = this.leavingView.pageRef().nativeElement;
const wrapper = new Animation(this.plt, ele.querySelector('.modal-wrapper'));
const contentWrapper = new Animation(this.plt, ele.querySelector('.wrapper'));
wrapper.beforeStyles({ 'transform': 'translateX(100%)', 'opacity': 1 });
wrapper.fromTo('transform', 'translateX(0)', 'translateX(100%)');
wrapper.fromTo('opacity', 1, 1);
contentWrapper.fromTo('opacity', 1, 0);
this
.element(this.leavingView.pageRef())
.duration(500)
.easing('cubic-bezier(.1, .7, .1, 1)')
.add(contentWrapper)
.add(wrapper);
}
}
Then import those modules in app.module.ts
export class AppModule {
constructor(public config: Config) {
this.setCustomTransitions();
}
private setCustomTransitions() {
this.config.setTransition('modal-translate-up-enter', ModalTranslateEnterTransition);
this.config.setTransition('modal-translate-up-leave', ModalTranslateLeaveTransition);
}
}
And create modal using following options:
var modal = this.modalCtrl.create(AddToCartModalPage, {
productId: this.productId,
skuId: this.skuId,
zipcode: this.zipcode,
sellerProfileId: this.sellerProfileId,
branchId: this.branchId,
changeSeller: this.changeSeller
}, {
showBackdrop: false,
enableBackdropDismiss: false,
cssClass: 'add-to-cart-modal',
enterAnimation: 'modal-translate-up-enter',
leaveAnimation: 'modal-translate-up-leave'
});
Find more information my article here: Blog
Find complete demo repository here: Github
回答2:
You can add your own animation css, ex:
.slide-in-right {
-webkit-transform: translateX(100%);
transform: translateX(100%); }
.slide-in-right.ng-enter, .slide-in-right > .ng-enter {
-webkit-transition: all cubic-bezier(0.1, 0.7, 0.1, 1) 400ms;
transition: all cubic-bezier(0.1, 0.7, 0.1, 1) 400ms; }
.slide-in-right.ng-enter-active, .slide-in-right > .ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0); }
.slide-in-right.ng-leave, .slide-in-right > .ng-leave {
-webkit-transition: all ease-in-out 250ms;
transition: all ease-in-out 250ms; }
and usage with 'slide-in-right'
the same for 'fade-in'
https://forum.ionicframework.com/t/slide-in-right-animation-for-ionicmodal/18882
回答3:
no ionic does not provide fade-in animation. But you can do that by using animate.css