【问题标题】:An example of Angular 2 animation ended callback functionAngular 2 动画结束回调函数示例
【发布时间】:2023-04-06 22:37:02
【问题描述】:

我正在尝试创建一个将在 Angular 2 中的动画结束时触发的函数(我使用的是最新的 Angular cli)。

我一直在 Angular Animations 上了解如何通过为触发器分配回调来实现这一点 在我的代码示例中,我有一个在页面上动画的组件。代码如下:

//first.component.ts

import { Component, OnInit } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/core';


@Component({
  selector: 'app-first',
  templateUrl: './first.component.html',
  styleUrls: ['./first.component.css'],
  host: {
    '[@routeAnimation]': 'true',
    '[style.display]': "'block'",
    '[style.position]': "'absolute'"
  },
  animations: [
    trigger('routeAnimation', [
      state('*', style({transform: 'translateX(0)', opacity: 1})),
      transition('void => *', [style({transform: 'translateX(-100%)', opacity: 0}),animate(500)]),
      transition('* => void', animate(500, style({transform: 'translateX(100%)', opacity: 0})))
    ])
  ]
})
export class FirstComponent implements OnInit {

  constructor() { }

  ngOnInit() {

  }

  myFunc() {
  // call this function at the end of the animation.
 }

}

html 只是一个 div

<div class="w9914420">
  <h2>
     first-component Works!
  </h2>
</div> 

说实话,我对 JavaScript 不太熟悉,所以任何帮助或快速示例都可以帮助我更好地了解 Angular 2。

【问题讨论】:

    标签: angular angular-ui-router


    【解决方案1】:

    这是工作示例:

    import {Component, NgModule, Input, trigger, state, animate, transition, style, HostListener } from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'
    
    @Component({
      selector : 'toggle',
      animations: [
        trigger('toggle', [
          state('true', style({ opacity: 1; color: 'red' })),
          state('void', style({ opacity: 0; color: 'blue' })),
          transition(':enter', animate('500ms ease-in-out')),
          transition(':leave', animate('500ms ease-in-out'))
        ])
      ],
      template: `
      <div class="toggle" [@toggle]="show" 
            (@toggle.start)="animationStarted($event)"
            (@toggle.done)="animationDone($event)"
         *ngIf="show">
        <ng-content></ng-content>
      </div>`
    })
    export class Toggle {
      @Input() show:boolean = true;
      @HostListener('document:click')
      onClick(){
        this.show=!this.show;
      }
    
      animationStarted($event) {
        console.log('Start');
      }
    
      animationDone($event) {
        console.log('End');
      }
    }
    
    @Component({
      selector: 'my-app',
      template: `
        <div>
          <toggle>Hey!</toggle>
        </div>
      `,
    })
    export class App {
    
    }
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ App, Toggle ],
      bootstrap: [ App ]
    })
    export class AppModule {}
    

    Plunker

    【讨论】:

    • 在 NgModule 中使用这种方法的好例子,谢谢 PatrickJane。
    • 我实际上创建了一个 PLUNKER (plnkr.co/edit/d1UQcN?p=preview) 并实现了回调函数并将 start 和 done 触发器添加到 user/component.ts 文件中。我注意到 User 组件转换有一个奇怪的行为。当组件在舞台上转换以移除仪表板组件时,此实现会导致轻微延迟。关于为什么会发生这种情况的任何想法?删除回调实现会使事情恢复正常?
    • 看看我的plunker 告诉我您是否发现与我上述评论中所述相同的问题
    【解决方案2】:

    现在 Angular2 支持 (@animation.start)(@animation.done) 来利用动画事件。

    例如可以在first.component.html中使用(@routeAnimation.start)=onStart($event)(@routeAnimation.done)=onDone($event)

    您可以通过here了解更多信息。

    您还可以在Plunker 上看到一个带有 first.component.ts 的简单示例。

    希望对您有所帮助!

    【讨论】:

    • 那么你将如何在 first.component.ts 中定义 routeAnimation onstart 和 done 函数?
    • 我很欣赏这个例子,并演示了我将如何用我的代码实现
    • 实际上创建了一个 PLUNKER (plnkr.co/edit/d1UQcN?p=preview) 并实现了回调函数并将 start 和 done 触发器添加到 user/component.ts 文件中。我注意到 User 组件转换有一个奇怪的行为。当组件在舞台上转换以移除仪表板组件时,此实现会导致轻微延迟。关于为什么会发生这种情况的任何想法?删除回调实现会使事情恢复正常?
    • 看看我的plunker 告诉我您是否发现与我上述评论中所述相同的问题
    猜你喜欢
    • 2016-11-19
    • 2010-09-22
    • 1970-01-01
    • 2017-06-07
    • 2020-07-09
    • 2012-02-08
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多