3

I have two components that already have a working fade out/in animation on route change, and an outside background-image that fades in from a black background(done in css) on page-load.

app.component.html

<div class="bg"></div>
<div class="bgImage"></div>
<div class="main" [@fadeAnimation]="getDepth(myOutlet)">
  <router-outlet #myOutlet="outlet"></router-outlet>
</div>

app.component.ts

import { Component } from '@angular/core';
import { fadeAnimation } from './fade.animation';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
  ,  animations: [fadeAnimation]
})
export class AppComponent {
  title = 'app';

  getDepth(outlet){
    return outlet.activatedRouteData['depth'];
  }
}

I am trying to also make the background-image blur/unblur on route change.
Here is the animation service with my logic for what the blur animation should look like removed.

fade.animation.ts

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

export const fadeAnimation =
    trigger('fadeAnimation', [
        transition( '1 => 2', [
            query(':enter', 
                [
                    style({ opacity: 0 })
                ], 
                { optional: true }),
        // query('.bgImage', 
        //     [
        //         style({ filter: 'blur(0px)' }),
        //         animate('1s ease-in-out', style({ filter: 'blur(5px)' }))
        //     ], 
        //     { optional: true }
        // ),
            query(':leave', 
                [
                    style({ opacity: 1 }),
                    animate('0.25s ease-in-out', style({ opacity: 0 }))
                ], 
                { optional: true }),
            query(':enter', 
                [
                    style({ opacity: 0 }),
                    animate('0.25s ease-in-out', style({ opacity: 1 }))
                ], 
                { optional: true })
        ]),
        transition( '2 => 1', [
            query(':enter', 
                [
                    style({ opacity: 0 })
                ], 
                { optional: true }),
        // query('.bgImage', 
        //     [
        //         style({ filter: 'blur(5px)' }),
        //         animate('1s ease-in-out', style({ filter: 'blur(0px)' }))
        //     ], 
        //     { optional: true }
        // ),
            query(':leave', 
                [
                    style({ opacity: 1 }),
                    animate('0.25s ease-in-out', style({ opacity: 0 }))
                ], 
                { optional: true }),
            query(':enter', 
                [
                    style({ opacity: 0 }),
                    animate('0.25s ease-in-out', style({ opacity: 1 }))
                ], 
                { optional: true })
        ])
    ]);

1 Answers1

1

Not sure if I understand the question correctly.

Define a trigger blur.

trigger('blur', [
      state('1', style({filter: 'blur(0px)'})),
      state('2', style({filter: 'blur(8px)'})),
      transition('1=>2', [ animate('1s')]),
      transition('2=>1', [ animate('1s')])
    ])

And in HTML:

<div class="bgImage" [@blur]="getDepth(myOutlet)" ></div>

check it here. https://stackblitz.com/edit/angular7-material-primeng-template-31yvug

Haijin
  • 2,561
  • 2
  • 17
  • 30