【发布时间】:2021-10-23 16:46:50
【问题描述】:
我有一个指令应该为 div 内容设置动画。动画效果很好,但每次导航后都会重播。有没有办法让它只在初始页面加载时运行?
**编辑
我正在制作动画的元素在导航时重新渲染。
@Directive({
selector: '[appAnimateText]'
})
export class AnimateTextDirective implements AfterViewInit, OnInit {
@HostBinding('style.white-space') whiteSpace = 'pre';
private animateTo: string | undefined;
constructor(private el: ElementRef) {
}
get innerText(): string {
return this.el.nativeElement.innerText;
}
set innerText(newText: string) {
this.el.nativeElement.innerText = newText;
}
ngOnInit(): void {
this.animateTo = this.innerText;
// placeholder nbsp string which is than transformed back to the text
this.innerText = this.innerText.split('').map(() => ' ').join('');
}
ngAfterViewInit(): void {
if (this.animateTo) {
this.animate(this.animateTo.split(''));
}
}
animate(animateTo: string[]): Promise<void> {
// animation placeholder
return new Promise((res) => setTimeout(res, 2000))
}
【问题讨论】:
-
这里没有足够的信息。导航后是否会重新渲染您使用的元素?