Skip to main content

Router transition animation in angular


Hello friends, This is my  tutorial on Angular.
In this  tutorial ,you will learn about Angular router transition animation.
Follow these steps to create animation in angular.
First You need to enable the animations module by importing BrowserAnimationsModule .
Write below code in your app.module.ts file
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Next you need to create animation.ts file and write below code .
import {
  animate,
  query,
  style,
  transition,
  trigger,
  group,
} from '@angular/animations';

export function routerAnimation() {
  return trigger('routerAnimation', [
    // One time initial load. Move page from left -100% to 0%
    transition('-1 => *', [
      query(':enter', [
        style({
          position: 'fixed',
          width: '100%',
          transform: 'translateX(-100%)',
        }),
        animate(
          '500ms ease',
          style({
            opacity: 1,
            transform: 'translateX(0%)',
          }),
        ),
      ]),
    ]),

    // Previous, slide left to right to show left page
    transition(':decrement', [
      // set new page X location to be -100%
      query(
        ':enter',
        style({
          position: 'fixed',
          width: '100%',
          transform: 'translateX(-100%)',
        }),
      ),

      group([
        // slide existing page from 0% to 100% to the right
        query(
          ':leave',
          animate(
            '500ms ease',
            style({
              position: 'fixed',
              width: '100%',
              transform: 'translateX(100%)',
            }),
          ),
        ),
        // slide new page from -100% to 0% to the right
        query(
          ':enter',
          animate(
            '500ms ease',
            style({
              opacity: 1,
              transform: 'translateX(0%)',
            }),
          ),
        ),
      ]),
    ]),

    // Next, slide right to left to show right page
    transition(':increment', [
      // set new page X location to be 100%
      query(
        ':enter',
        style({
          position: 'fixed',
          width: '100%',
          transform: 'translateX(100%)',
        }),
      ),

      group([
        // slide existing page from 0% to -100% to the left
        query(
          ':leave',
          animate(
            '500ms ease',
            style({
              position: 'fixed',
              width: '100%',
              transform: 'translateX(-100%)',
            }),
          ),
        ),
        // slide new page from 100% to 0% to the left
        query(
          ':enter',
          animate(
            '500ms ease',
            style({
              opacity: 1,
              transform: 'translateX(0%)',
            }),
          ),
        ),
      ]),
    ]),
  ]);

}
Next in your app-routing.module.ts file,write below code where routes define.
For Example:
{ path: 'abc', component: AbcComponent, data: { num: 1 } }
{ path: 'test', component: TestComponent, data: { num: 2 } }

Next in your app.component.ts file, Import routerAnimation file and RouterOutlet.
you need to write below code,

import { routerAnimation } from './common/animations';
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [routerAnimation()],
})
export class AppComponent {
  constructor() {}
  public getRouteAnimation(outlet: RouterOutlet) {
    const res =
      outlet.activatedRouteData.num === undefined
        ? -1
        : outlet.activatedRouteData.num;
    return res;
  }
}
And then in app.component.html file . you need to write below code,
<div class="navbar">
  <a [routerLink]="['abc']">Abc</a>
  <a [routerLink]="['test']">Test</a>
</div>
<div class="content" [@routerAnimation]="getRouteAnimation(router)">
  <router-outlet #router="outlet"></router-outlet>
</div>
Follow all of the above steps to make animation in your angular projects.
I hope You like this tutorial.
Thanks For Reading.

Comments

Post a Comment

Popular posts from this blog

Angular 7 Files Explanation

Hello friends, This is my third tutorial on Angular 7. In this Angular 7 tutorial ,you will learn about Angular 7 App Files Explanation. Angular 7 App Files Explanation See the structure of the Angular 7 app on  Visual Studio Code IDE  (how it looks on IDE). For Angular 7 development, you can use either Visual Studio Code IDE or WebStorm IDE. Both are good. Here, we are using  Visual Studio Code IDE .   Files used in Angular 7 app folder Angular 7 App files which are mainly used in your project are given below: src folder :   This is the folder which contains the main code files related to your angular application. app folder:  The app folder contains the files, you have created for app components. app.component.css:  This file contains the cascading style sheets code for your app component. app.component.html:  This file contains the html file related to app component. This is the template file which is used by angular to do...