【问题标题】:Pause Angular Animations暂停角度动画
【发布时间】:2018-07-20 16:13:17
【问题描述】:

是否可以在 Angular 2+ 中暂停动画?我想在将鼠标悬停在元素上时暂停动画,并在鼠标移出时从停止的地方恢复动画。

我创建了一个简单的脚本来演示:https://stackblitz.com/edit/scrolling-text

这是我的组件:

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

@Component({
  selector: 'my-app',
  template: `
    <div class="container">
      <div class="scrolling-text" [@scroll]="state" (@scroll.done)="scrollDone()">Hover to pause!</div>
    </div>
  `,
  styles: [`
    .container {
      height: 30px;
      overflow: hidden;
      position: relative;
      width: 100%;
    }

    .scrolling-text {
      position: absolute;
      white-space: nowrap;
    }

    /* Below doesn't work to pause */

    .scrolling-text:hover, .container:hover {
      -moz-animation-play-state: paused;
      -webkit-animation-play-state: paused;
      animation-play-state: paused;
    }
  `],
  animations: [
    trigger('scroll', [
      state('on', style({left: '-100px'})),
      transition('* => *', [
        style({left: '-100px'}),
        animate(10000, style({left: '100%'}))
      ])
    ])
  ]
})
export class AppComponent  {
  state = 0;

  scrollDone() {
    this.state++;
  }

}

我试过animation-play-state: paused; 没有运气:

.scrolling-text:hover, .container:hover {
  -moz-animation-play-state: paused;
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}

有什么方法可以让它工作吗?

【问题讨论】:

    标签: angular css-animations angular5 angular-animations


    【解决方案1】:

    我找到了使用AnimationBuilder 的方法。

    import { Component, ElementRef, ViewChild } from '@angular/core';
    import { style, animate, AnimationBuilder, AnimationPlayer } from '@angular/animations';
    
    @Component({
    selector: 'my-app',
    template: `
      <div class="container" (mouseover)="player.pause()" (mouseout)="player.play()">
        <div #el class="scrolling-text">Hover to pause!</div>
      </div>
    `,
    styles: [`
      .container {
        height: 30px;
        overflow: hidden;
        position: relative;
        width: 100%;
      }
    
      .scrolling-text {
        position: absolute;
        white-space: nowrap;
      }
    `]
    })
    export class AppComponent  {
    
      @ViewChild('el') el: ElementRef;
    
      private factory = this.builder.build([
        style({left: '-100px'}),
        animate(10000, style({left: '100%'}))
      ]);
      private player;
    
      constructor(private builder: AnimationBuilder) { }
    
      ngOnInit() {
        this.player = this.factory.create(this.el.nativeElement, {});
        this.animate();
      }
    
      private animate() {
        this.player.reset();
    
        this.player.onDone(() => {
          this.animate();
        });
    
        this.player.play();
      }
    
    }
    

    Live Demo

    【讨论】:

      【解决方案2】:

      虽然不是最好的解决方案。 自从

      this.player = this.factory.create(this.el.nativeElement, {}); 
      

      在 RendererAnimationPlayer 中创建另一个 WebAnimationsPlayer 实例

      您可以通过以下方式自行检查:

       console.log(this.player._renderer.engine.players.length);
      

      当然,在您的情况下,这并不重要,但仍然如此。 当您尝试在诸如 mousemove 事件中重用动画时,这就会发挥作用。然后 Angular 最多可以创建 1000 个玩家。并溢出内存。

      如果您想重用动画以提高内存效率,您应该重用现有播放器。

      或致电

       if (this.player) {
            this.player.destroy();
       }
      

      之前

      this.player = this.factory.create(this.el.nativeElement, {}); 
      

      【讨论】:

      • 感谢您的回答,谢尔盖。这是很好的信息。实际上,我已经修改了我的演示以重用播放器,但看起来我从未更新过我的答案。我现在已经这样做了。
      猜你喜欢
      • 1970-01-01
      • 2016-03-28
      • 2013-11-25
      • 2012-08-21
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      相关资源
      最近更新 更多