css - Angular - 在路由更改时为外部元素设置动画

标签 css angular angular-router angular-animations

我有两个组件,它们已经在路由更改时有一个有效的淡出/淡入动画,以及一个在页面加载时从黑色背景淡入(在 css 中完成)的外部背景图像。

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'];
  }
}

我也在尝试在路线更改时使背景图像模糊/不模糊。
这是动画服务,我的逻辑是删除模糊动画应该是什么样子。

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 })
        ])
    ]);

最佳答案

不确定我是否理解正确。

定义一个触发器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')])
    ])

在 HTML 中:

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

在这里检查。 https://stackblitz.com/edit/angular7-material-primeng-template-31yvug

关于css - Angular - 在路由更改时为外部元素设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52668996/

相关文章:

css - 垂直居中工作,IE7 除外(表格单元格)

c# - 使用代理开发服务器时,.net core spa 模板应用程序中 "Fetch data"上出现 404

javascript - 如何在 Chart.js y 轴标签旁边添加输入框?

angular - 在带有路由的模块中导入带有路由的延迟加载模块会中断路由

javascript - 一种输入 - 两种风格?左侧光标 :text, 右侧光标 :pointer?

CSS3 - 过渡延迟仅在关闭时?

javascript - Rxjs : prevent pushing data to subjects from outisde the service

Angular 2+ 静态 html 路径

angular - 你如何模拟 ActivatedRoute

CSS视觉轻弹