Please or Register to create posts and topics.

Ionic: Custom page transitions

Default page transitions that you can find in out of the box Ionic framework does not always suits your needs. In some cases you may want to have custom page animations when ion-router-outlet navigates between pages. Let me share with you a very compact and elegant solution for the title topic.

Lets suppose that you want to add fade out effect to all page transitions in your Ionic application. The best place to customize animation for this purpose is ion-router-outlet. According to the Ionic documentation you can pass a custom function as the "animation" property of ion-router-outlet.

Edit app.component.html file and set "myCustomPageTransition" value as the animation property of ion-router-outlet.

  <ion-router-outlet [animation]="myCustomPageTransition"></ion-router-outlet>

Then edit app.component.ts file and create myCustomPageTransition property as is shown below. Please not that you can use both enteringEl and leavingEl in your animation.

import { Component } from '@angular/core';

import { AnimationController, Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
export class AppComponent {
myCustomPageTransition = ((baseEl: any, opts?: any) => { 
    console.log("opts.enteringEl:"  + opts.enteringEl); //Entering Element - New Page
    console.log("opts.leavingEl:"  + opts.leavingEl);   //Leaving Element - Current Page
    var anim1 = this.animationCtrl.create()
      .fromTo('opacity', '1', '0.0')
    var anim2 = this.animationCtrl.create()
      .fromTo('opacity', '0.0', '1')
     var anim2 = this.animationCtrl.create()
      .addAnimation([anim1, anim2]);
    return anim2;

    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private animationCtrl: AnimationController
  ) {

  initializeApp() {
    this.platform.ready().then(() => {