【问题标题】:Animate routes based on route paths in angular (v2+)基于角度的路线路径动画路线(v2+)
【发布时间】:2017-09-03 09:58:33
【问题描述】:

而不是像this StackOverflow answerfirst part of the official documentation 那样将动画附加到被路由到的每个组件。一个例子:

hero-detail.component.ts:

import { Component, OnInit } from '@angular/core';
import { fadeInOutAnimation } from "app/app-routing.animation";

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  animations: [
    fadeInOutAnimation(300)
  ]
})
export class HomeComponent{       

}

app-routing.animation.ts:

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

export const fadeInOutAnimation = function (fadeInTimeMS) {
  return trigger('fadeInOut', [
    transition(':enter', [   // :enter is alias to 'void => *'
      style({ opacity: 0 }),
      animate(fadeInTimeMS, style({ opacity: 1 }))
    ]),
    transition(':leave', [   // :leave is alias to '* => void'
      animate(fadeInTimeMS, style({ opacity: 0 }))
    ])
  ])
}

我想根据路线路径为路线制作动画:

将路线动画应用于单个组件适用于一个简单的演示,但在现实生活中的应用程序中,最好根据路线路径为路线设置动画。

如角度文档中'Adding animations to the routed component' 末尾所述。但它没有详细说明如何执行此操作。

【问题讨论】:

  • 他们的指南充满了可疑的建议和类似的陈述。它变化如此之快,以至于您可以在几行之间找到相互矛盾的信息。如果有一些关于基于路线路径动画路线的信息,那就太好了。也许在“动画”部分或“路由”部分。

标签: javascript angular animation


【解决方案1】:

这是一个动画,当您前进时,您可以看到视图从左向右滑动,当您后退时,视图从右向左滑动无需单独将动画添加到组件

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

const leftToRight: AnimationMetadata[] = [
    /* order */
    /* 1 */ query(':enter, :leave', style({position: 'fixed', width: '100%'})
        , {optional: true}),
    /* 2 */ group([  // block executes in parallel
        query(':enter', [
            style({transform: 'translateX(100%)'}),
            animate('0.5s ease-in-out', style({transform: 'translateX(0%)'}))
        ], {optional: true}),
        query(':leave', [
            style({transform: 'translateX(0%)'}),
            animate('0.5s ease-in-out', style({transform: 'translateX(-100%)'}))
        ], {optional: true}),
    ])
];
const rightToLeft: AnimationMetadata[] = [
    /* order */
    /* 1 */ query(':enter, :leave', style({position: 'fixed', width: '100%'})
        , {optional: true}),
    /* 2 */ group([  // block executes in parallel
        query(':enter', [
            style({transform: 'translateX(-100%)'}),
            animate('0.5s ease-in-out', style({transform: 'translateX(0%)'}))
        ], {optional: true}),
        query(':leave', [
            style({transform: 'translateX(0%)'}),
            animate('0.5s ease-in-out', style({transform: 'translateX(100%)'}))
        ], {optional: true}),
    ])
];

export const routerTransition = trigger('routerTransition', [
    transition('first_page => second_page', leftToRight),
    transition('second_page => first_page ', rightToLeft),
    transition('second_page => third_page', leftToRight),
    transition('third_page => second_page', rightToLeft),
]);

并将其导入您的 AppComponent 并添加一个函数以返回路由状态。不要忘记我应用的样式。

import {routerTransition} from "./router.animations";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [`
        /deep/ router-outlet ~ * {
            position: absolute;
            width: 100%;
        }
    `],
    animations: [routerTransition]
})
export class AppComponent implements {
    getState(outlet) {
        return outlet.activatedRouteData.state;
    }

    onDeactivate() {
        // document.body.scrollTop = 0;
        // Alternatively, you can scroll to top by using this other call:
        window.scrollTo(0, 0);
    }
}

将其作为指令应用到主标签

<main [@routerTransition]="getState(o)">
    <router-outlet #o="outlet" (deactivate)="onDeactivate()"></router-outlet>
</main>

【讨论】:

    【解决方案2】:

    下面是一个基于路径改变过渡的例子:

    import {
        trigger,
        query,
        group,
        animate,
        transition,
        style
    } from "@angular/animations";
    
    const fade: any = [
        transition(':enter', [   // :enter is alias to 'void => *'
          style({ opacity: 0 }),
          animate(fadeInTimeMS, style({ opacity: 1 }))
        ]),
        transition(':leave', [   // :leave is alias to '* => void'
          animate(fadeInTimeMS, style({ opacity: 0 }))
        ])
    ];
    const transition2: any = [
        //other transition
    ];
    
    export const routerTransition = trigger('routerTransition', [
        transition('path1 => *', fade),
        transition('path2 => path3', fade),
        transition('path3 => path1', transition2),
        transition('* => path2', transition2),
    ]);
    

    然后在组件中使用动画:

    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
      animations: [
        routerTransition
      ]
    })
    

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多