【问题标题】:Angular : Animation complete callbackAngular:动画完成回调
【发布时间】:2017-04-16 03:39:52
【问题描述】:

我知道(@myTrigger.done) 事件,但即使动画被中断且未完成,它每次都会触发。

我想为一个弹出组件创建一系列动画,其中弹出组件从窗口右侧滑动,然后弹出关闭按钮。 弹窗也可以通过Esc键关闭。

我正在使用@myTrigger.done 触发事件来检查动画是否完成。但是如果我在弹出窗口滑动时按下Esc 键,它会发出完成事件,并为slideIn 动画发出完成事件,即使滑动动画未完成,也会弹出关闭按钮。

这里是plunker demo of my popup。您可以看到,当我点击背景或按Esc 键打开它时,会弹出关闭按钮,如果滑动动画被中断,则不会弹出该按钮。

那么有什么方法可以检查动画是完成还是中断?

【问题讨论】:

    标签: angular angular2-animation


    【解决方案1】:

    看起来角度/动画doesn't 除了开始/完成之外还有任何事件。 虽然,您可以保存开始时间,然后计算经过时间:

      started: number;
      popupStateChanging(event: AnimationEvent) {
        this.started = (new Date()).getTime();
        ///
      }    
      popupStateChanged(event: AnimationEvent) {
        const now = (new Date()).getTime();
        console.log(`${event.fromState}->${event.toState}: expected: ${event.totalTime}, actual:`, now - this.started);
        ///
      }
    

    此代码示例将产生以下输出:

    void->active: expected: 350, actual: 176 <--interruped
    active->inactive: expected: 350, actual: 351
    void->active: expected: 350, actual: 38  <--- interrupted
    active->inactive: expected: 350, actual: 353
    

    现场演示https://plnkr.co/edit/aGhGqHcYAU5wVsQWqAuB?p=preview

    【讨论】:

    • @HirenParekh 有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多