1、为什么有的人路由转动效离场动效不生效?

自己研究发现是加动效的位置放错了  如下:

  1. <---! animate-state.component.html -->
  2. <div style="background-color: antiquewhite;width: 100vw" [@trigger] id="f">
  3. style="background-color: antiquewhite;width: 100%;height: 100px;
  4. </div>
  5. //有的人可能会在组件内容的最顶层元素 加一个动效。以为这样能达到路由转场的效果,其实不然,因为组件本身才是这个组件的根元素。当通过路由转场时,
    //离场的根元素瞬间就隐藏了,以有其组件里面的内容不管加什么动效也无用,有动效,元素隐藏了也看不到啊!
    // 如上面的的html 模块名为:selector: 'app-animate-state',那么<app-animate-state></app-animate-state>是根元素,动画应该加在这样面才会有转场效果,而不是上面的组件内容最顶层元素
  1.  

2、解决放法

  因为路由你是没法写代表一个路由内容<app-animate-state></app-animate-state>这个标签元素的。

那么怎么做了?如下

用 @HostBinding()绑定根元素,加上动效。

  1. /路由切换动效如下
  2.  
  3. //animate-state.component.html
  4. <div style="background-color: antiquewhite;width: 100vw">
  5. style="background-color: antiquewhite;width: 100%;height: 100px;
  6. </div>
  7.  
  8. //animate-state.component.ts
  9.  
  10. import {Component, HostBinding, OnInit} from '@angular/core';
  11. import {trigger2} from "../animation/trigger";
  12.  
  13. @Component({
  14. selector: 'app-animate-state',
  15. templateUrl: './animate-state.component.html',
  16. styleUrls: ['./animate-state.component.css'],
  17. animations: [trigger2]
  18. })
  19. export class AnimateStateComponent implements OnInit {
  20.  
  21. constructor() { }
  22. @HostBinding('@trigger2') trigger2 = "";
  23. ngOnInit() {
  24. }
  25.  
  26. }
  27.  
  28. //animate-trigger.component.html
  29. <div style="background-color: red;width: 100vw">
  30. [@heroState] = 'name'
  31. </div>
  32.  
  33. //animate-trigger.component.ts
  34.  
  35. import {Component, HostBinding, OnInit} from '@angular/core';
  36. import {trigger1, trigger2} from "../animation/trigger";
  37.  
  38. @Component({
  39. selector: 'app-animate-trigger',
  40. templateUrl: './animate-trigger.component.html',
  41. styleUrls: ['./animate-trigger.component.css'],
  42. animations: [trigger1, trigger2]
  43. })
  44. export class AnimateTriggerComponent implements OnInit {
  45. constructor() { }
  46. @HostBinding('@trigger2') trigger2 = "";
  47. ngOnInit() {
  48. }
  49.  
  50. }
  51. //app.module.ts
  52. const appRoutes: Routes = [
  53. { path: 'trigger', component: AnimateTriggerComponent },
  54. { path: 'state', component: AnimateStateComponent },
  55. { path: '',
  56. redirectTo: '/trigger',
  57. pathMatch: 'full'
  58. },
  59. { path: '**', component: AnimateTriggerComponent }
  60. ];
  61. //app.component.html
  62.  
  63. <a routerLink="/trigger" routerLinkActive="active">trigger</a>
  64. <a routerLink="/state" routerLinkActive="active">state</a>
  65.  
  66. <div id="app">
  67. <router-outlet></router-outlet>
  68. </div>
  69.  
  70. //trigger.ts 动效
  71.  
  72. export const trigger2 = trigger('trigger2', [
  73. transition('* => void', [style({opacity: ,position: 'absolute'}),animate(,style({opacity: }))]),
  74. transition('void => *', [style({opacity: ,position: 'absolute'}),animate(,style({opacity: }))])
  75. ]);

3、可是这样做的话,每个路由都要这样加,会显的很繁琐,在重复,有办法一次性全加上吗? 当然是有  如下: 用到动效里的query特性函数和路由中的NavigationEnd属性 代码如下

  1. //animate-state.component.html
  2. <div style="background-color: antiquewhite;width: 100vw">
  3. style="background-color: antiquewhite;width: 100%;height: 100px;
  4. </div>
  5.  
  6. //animate-state.component.ts
  7.  
  8. import {Component, HostBinding, OnInit} from '@angular/core';
  9.  
  10. @Component({
  11. selector: 'app-animate-state',
  12. templateUrl: './animate-state.component.html',
  13. styleUrls: ['./animate-state.component.css'],
  14. })
  15. export class AnimateStateComponent implements OnInit {
  16.  
  17. constructor() { }
  18. ngOnInit() {
  19. }
  20.  
  21. }
  22.  
  23. //animate-trigger.component.html
  24. <div style="background-color: red;width: 100vw">
  25. [@heroState] = 'name'
  26. </div>
  27.  
  28. //animate-trigger.component.ts
  29.  
  30. import {Component, HostBinding, OnInit} from '@angular/core';
  31.  
  32. @Component({
  33. selector: 'app-animate-trigger',
  34. templateUrl: './animate-trigger.component.html',
  35. styleUrls: ['./animate-trigger.component.css'],
  36. })
  37. export class AnimateTriggerComponent implements OnInit {
  38. constructor() { }
  39. ngOnInit() {
  40. }
  41.  
  42. }
  43. //trigger.ts
  1. import {
    trigger,
    state,
    style,
    animate,
    transition, query, group
    } from '@angular/animations';
    import {AnimationEntryMetadata} from "@angular/core";
  1. export const routeAnimation1: AnimationEntryMetadata = trigger('routeAnimation1', [ transition('* => *', group([ query(':leave', animate('.5s',
    style({opacity: ,position: 'absolute'})), { optional: true }), query(':enter', [style({opacity: ,position: 'absolute'}),
    animate('.5s', style({ opacity: }))],{ optional: true }) ]) ) ]);
  2.  
  3. //app.module.ts
    const appRoutes: Routes = [ { path: 'trigger', component: AnimateTriggerComponent },
    { path: 'state', component: AnimateStateComponent }, { path: '', redirectTo: '/trigger', pathMatch: 'full' } ];
  4.  
  5. //app.component.html
  6.  
  7. <a routerLink="/trigger" routerLinkActive="active">trigger</a>
    <a routerLink="/state" routerLinkActive="active">state</a>
    <div id="app" [@routeAnimation1]="routerStateCode">
      <router-outlet>
      </router-outlet>
    </div>
  8.  
  9. //app.component.ts
    import { Component } from '@angular/core'; import {NavigationEnd, Router} from "@angular/router";
    import { routeAnimation1} from "./animation/trigger";
    @Component({ selector: 'app-root', templateUrl:
    './app.component.html', styleUrls: ['./app.component.css'],
    animations: [routeAnimation1] })
    export class AppComponent {
    routerState:boolean = true;
    routerStateCode:string = 'active';
    constructor(private router:Router)
    { this.router.events.subscribe(event => {
    if (event instanceof NavigationEnd) {
  10.  
  11. // 每次路由跳转改变状态
    console.log(NavigationEnd,event)
  12.  
  13. this.routerState = !this.routerState;
    this.routerStateCode = this.routerState ? 'active' : 'inactive'; } }); } }

有问题,欢迎留言。。。

angular2中的路由转场动效的更多相关文章

  1. Vue-admin工作整理(七):路由的切换动效

    思路就是通过 transition-group 这个组件来对多组件进行控制,效果是通过样式来写,transition-group要有指定的key值,样式中通过name来命名类的名字 <templ ...

  2. angular2-4 之动效-animation

    提示:   angular2 时animation代码在核心模块里面(@angular/core里面);到了angular4.0时animation从核心模块中提取出来作为一个单独的模块, 这样可以在 ...

  3. 动效解析工厂:Mask 动画

    转载自:http://www.cocoachina.com/ios/20160214/15250.html 前言:很多动效都是多种动画的组合,有时候你可能只是需要其中某个动画,但面对庞杂的代码库或是教 ...

  4. 百度MUX:APP动效之美需内外兼修

    移动互联网时代已经到来.APP已如天空的繁星.数也数不清.随着手机硬件的不断升级,实现炫酷且流畅的动效不再是遥远的梦想.假设你是APP达人,喜欢试用各种APP,你肯定会发现越来越多的APP開始动效化. ...

  5. iOS 停止不必要的UI动效设计

    http://www.cocoachina.com/design/20151124/14400.html 前言:这篇短文将会探讨UI设计中动画的过度使用,并将其与早期的视觉设计进行对比,提出一些对于有 ...

  6. 前端必须收藏的CSS3动效库!!!

    现在的网站和App的设计中越来越重视用户体验,而优秀的动效则能使你的应用更具交互性,从而吸引更多用户的使用. 如果你对CSS3中定义动效还不熟练,或希望采用更加简单直接的方式在你的应用中引入动效的话, ...

  7. 【总结】前端必须收藏的CSS3动效库!!!

    现在的网站和App的设计中越来越重视用户体验,而优秀的动效则能使你的应用更具交互性,从而吸引更多用户的使用. 如果你对CSS3中定义动效还不熟练,或希望采用更加简单直接的方式在你的应用中引入动效的话, ...

  8. 巧用 -webkit-box-reflect 倒影实现各类动效

    在很久之前的一篇文章,有讲到 -webkit-box-reflect 这个属性 -- 从倒影说起,谈谈 CSS 继承 inherit -webkit-box-reflect 是一个非常有意思的属性,它 ...

  9. Google I/O 官方应用中的动效设计

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/82881743 作者:Nick Butcher, ...

随机推荐

  1. java_基础——用代码编译.java文件+加载class文件

    [本文介绍] 本文不是深入理解和使用java编译器,只是在代码里编译.java文件的helloWorld.这种技术还是蛮有意思的,说不定在将来的某些只能化项目会运用到!^_^ [简单编译的流程] [j ...

  2. Spring第一弹—-全面阐述Spring及轻重量级容器的划分

    Spring是什么? Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架,它的主要目得是简化企业开发. IOC 控制反转 :   1 ...

  3. tkprof参数详解

    tkprof参数详解 table=schema.table 指定tkprof处理sql trace文件时临时表的模式名和表名 insert=scriptfile 创建一个文件名为scriptfile的 ...

  4. lua在线手册汇总

    1. Lua官方参考手册 Lua 4.0 : http://www.lua.org/manual/4.0/Lua 5.0 : http://www.lua.org/manual/5.0/Lua 5.1 ...

  5. Java泛型三:Java泛型详解

    原文地址https://www.cnblogs.com/lzq198754/p/5780426.html 1.为什么需要泛型 泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇 ...

  6. Delphi APP 開發入門(三)簡易計算機

    Delphi APP 開發入門(三)簡易計算機 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:68 ...

  7. git安装教程(windows安装)

    git下载地址 https://git-scm.com/download/win 选择安装的组件,推荐全选 Additional icons 附加图标 ​ On the Desktop 在桌面上 Wi ...

  8. java中内存泄露和内存溢出

    内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory:比如申请了一个integer,但给它存了long才能存下的数,那就是内存溢出. ...

  9. C++ 顺序表实现

    线性表就是字面上的意思, 顺序表是线性表基于数组的一种实现, “顺序”这个名字怎么来的并不清楚,可以勉强解释为“存储地址是连续.顺序的”. 另外两种线性表实现分别是“基于链表”和“散列存储”. 顺序表 ...

  10. 【c++ primer, 5e】函数匹配

    练习 6.49 候选函数:与所调用的函数的名字相同的函数的集合. 可行函数:给候选函数加上参数数量.参数类型的约束所得到的函数的集合. 6.50 a 3.4可行,二义匹配 b 2.4可行,2是最佳匹配 ...