我正在为我的项目寻找无限动画。中没有这方面的文档angular.io
。所以我尝试了这种方法,花了我很多时间来实现这一目标。希望它能帮助其他人。
首先定义animation
在你的class
.
animations: [
trigger('move', [
state('in', style({transform: 'translateX(0)'})),
state('out', style({transform: 'translateX(100%)'})),
transition('in => out', animate('5s linear')),
transition('out => in', animate('5s linear'))
]),
]
然后将其插入你的html
<div [@move]="state" (@move.done)="onEnd($event)"></div>
然后设置state = 'in'
and in ngAfterViewInit()
生命周期挂钩,更新您的state
适当的价值'out'
with setTimeout
函数和结束回调函数后,更新你的state
财产价值转化为'in'
并检查你的state
财产价值,如果in
然后更新为out
by setTimeout
像这样的功能
export class AppComponent implements AfterViewInit {
state = 'in';
ngAfterViewInit() {
setTimeout(() => {
this.state = 'out';
}, 0);
}
onEnd(event) {
this.state = 'in';
if (event.toState === 'in') {
setTimeout(() => {
this.state = 'out';
}, 0);
}
}
}
快乐编码:)