Animating routes in angular 4

I am trying to animate route transitions in angular 4, the animation works when the page loads first, and the page refreshes, so I know that the animation works, but not when switching routes. What am I missing?

here is the code ...

// component metadata

animations: [fadeInAnimation]

// tempalte

<div class="route-container" [@fadeInAnimation]>
    <router-outlet></router-outlet>
</div>

// styles

.route-container {
  position:relative;
}
.route-container>* {
  display:block;
}

//animation

trigger('fadeInAnimation', [

    // route 'enter' transition
    transition(':enter', [

        // css styles at start of transition
        style({ opacity: 0 }),

        // animation and styles at end of transition
        animate('6s cubic-bezier(.35,0,.25,1)', style({ opacity: 1 }))
    ]),
]);
+6
source share
1 answer

To ensure that the proc animation is routed on each route, you will need to define the transitions between each route. The following is an example that I use to create a box effect when switching between my "home" route and my "acct" route:

import { trigger, animate, style, group, query, transition } from '@angular/animations';

export const baseAnimation =
    trigger('baseAnimation', [
      transition('acct => home', [
        query(':enter, :leave', style({ position: 'absolute', top: 0, left: 0, right: 0 })),
        query(':leave', style({ height: '*'})),
        query('.acct', [
            animate('300ms',
            style({ height: 0 }))
        ])
      ]),
      transition('home => acct', [
        query(':enter, :leave',
          style({ position: 'absolute', top: 0, left: 0, right: 0 })),
        query(':enter .acct', [
          style({ height: 0 }),
          animate('300ms', style({ height: '*' }))
        ])
      ])
    ])

, .acct ( ). , .

app.component.html :

<div [@baseAnimation]="prepareRouteTransition(outlet)">
  <router-outlet #outlet="outlet"></router-outlet>
</div>
</div>

app.component.ts :

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { baseAnimation } from './anim/base.animation';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [ baseAnimation ]
})

export class AppComponent {

  constructor() { }

  prepareRouteTransition(outlet) {
    const animation = outlet.activatedRouteData['animation'] || {};
    return animation['value'] || null;
  }
}
+2

All Articles