【问题标题】:Angular sequenced router animation角序列化路由器动画
【发布时间】:2019-01-15 06:57:45
【问题描述】:

我是角度动画的新手。我想测试一个具有多个动画阶段的路由器动画。

第一页(主页)有 3 个不同颜色的 div 块。如果我们点击其中一个,它将平滑地缩放到 100% 的宽度。

变成100%后如果需要在中间显示一些文字几秒钟。

然后下一页必须显示从中间到放大的方形动画

例如,它必须经过以下步骤:

必须显示放大框,因为下一页已经在第一页之后。

如需更多权限,请查看此websites 磁贴点击动画

我想通过角度动画来完成这项任务,而我是新手,不熟悉这种复杂的逐步动画。

【问题讨论】:

    标签: html css angular animation angular-animations


    【解决方案1】:

    我无法通过一个动画来实现这一点,但我使用了 2 个动画组件来完成。

    对于显示框动画,我不得不在应用组件中编写动画。

    app.component.html =>

    <app-header></app-header>
    
    <nav>
      <a routerLink="/home">Dashboard</a>
      <a routerLink="/next">next</a>
    </nav>
    <div id="page" class="routeContainer" [@routeAnimation]="getDepth(myOutlet)">
      <router-outlet #myOutlet="outlet"></router-outlet>
    </div>
    

    app.component.ts =>

    import { Component } from '@angular/core';
    import {animate, group, query, style, transition, trigger} from '@angular/animations';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
      animations: [
        trigger('routeAnimation', [
          transition('1 => 2', [
            // animate the leave page away
            group([
              query(
                ':enter',
                [
                  style({
                    position: 'absolute',
                    'z-index' : '100',
                    opacity: 1,
                    height: '100%',
                    width: '100%',
                    'clip-path': 'polygon(100px 400px, 400px 400px , 400px 500px, 100px 500px)'
    
                  }),
                  animate(
                    '3s cubic-bezier(.35,0,.25,1)',
                    style({
                      opacity: 1,
                      'clip-path': 'polygon(0px 0px, 100% 0px , 100% 100%, 0px 100%)'
                       })
                  ),
                ],
                { optional: true }
              ),
              query(
                ':leave',
                [animate('2s', style({ opacity: 1, 'z-index': '0', }))],
                { optional: true }
              )
            ])
          ]),
    
        ])
      ]
    })
    export class AppComponent {
      title = 'app';
    
      getDepth(outlet) {
        return outlet.activatedRouteData['depth'];
      }
    
    
    }
    

    在路由中我们声明每条路由的深度值,像这样 =>

    const routes: Routes = [
      { path: '', redirectTo: '/home', pathMatch: 'full' },
      { path: 'home', component: MainComponent, data: {depth: 1} },
      { path: 'next', component: NextComponent, data: {depth: 2} }
    
    ];
    

    对于窗帘动画,我在MainComponent里面单独写了动画。

    main.component.html

    <div class="row main-page">
    
    
      <div  [ngClass] ="{'link1-animation' :helpMenuOpen, 'col-sm link1' :!helpMenuOpen }"  (click)="toggle()"
            [@slideInOut]="helpMenuOpen"
            (@slideInOut.done)="animationDone($event)"
    
      >
    
        <h1>
          123
        </h1>
      </div>
    
      <div class="col-sm link2">
      </div>
    
      <div class="col-sm link3">
      </div>
    </div>
    

    main.component.ts

    @Component({
      selector: 'app-main',
      templateUrl: './main.component.html',
      styleUrls: ['./main.component.css'],
      animations: [
        trigger('slideInOut', [
          state('true', style({
            'z-index': 2,
            width: '100%',
            left: 0,
            'background-color': 'pink'
          })),
          transition('0 => 1', animate('1s ease'))
        ])
      ]
    })
    
    
    export class MainComponent implements OnInit {
    
      public helpMenuOpen = false;
    
      constructor(private router: Router) {
      }
    
      ngOnInit() {
      }
    
      toggle() {
        this.helpMenuOpen = !this.helpMenuOpen ;
    
      }
    
    
      animationDone($event) {
    
        if (this.helpMenuOpen) {
    
          this.router.navigate(['/next']);
        }
    
      }
    
    
    }
    

    我所做的是等到窗帘动画完成并导航到下一页。当导航发生时,我上面所做的路线动画将运行。

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2021-05-17
      • 2019-01-16
      • 1970-01-01
      • 2020-10-23
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多