本文介绍了暂停角度动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Angular 2+中暂停动画?我想在将鼠标悬停在某个元素上时暂停播放动画,并在鼠标悬停时从停下的位置继续播放动画.
Is it possible to pause animations in Angular 2+? I would like to pause an animation upon mousing over an element and resume the animation from where it left off when mousing out.
我创建了一个简单的脚本来演示: https://stackblitz.com/edit/scrolling-文字
I have created a simple script to demonstrate: 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;
}
有什么办法可以使它正常工作吗?
Is there any way to get this to work?
推荐答案
我找到了一种使用AnimationBuilder
的方法.
I found a way to do it with 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();
}
}
这篇关于暂停角度动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!