gpt4 book ai didi

angular - 暂停 Angular 动画

转载 作者:行者123 更新时间:2023-12-03 23:55:36 25 4
gpt4 key购买 nike

是否可以在 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;
}

有什么办法可以让它发挥作用吗?

最佳答案

我找到了一种方法来做 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

关于angular - 暂停 Angular 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48713488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com