【问题标题】:Angular - Animate outside element on route changeAngular - 在路线更改时为外部元素设置动画
【发布时间】:2019-02-18 08:42:16
【问题描述】:

我有两个组件在路由更改时已经有一个有效的淡出/淡入动画,以及一个在页面加载时从黑色背景淡入的外部背景图像(在 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 })
        ])
    ]);

【问题讨论】:

    标签: css angular angular-router angular-animations


    【解决方案1】:

    不确定我是否正确理解了这个问题。

    定义触发器模糊

    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

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 2017-09-03
      • 2014-03-09
      • 2015-02-01
      • 1970-01-01
      • 2021-02-03
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多