在杂草丛生了一段时间后,我想出了以下解决方案,供其他可能有兴趣做类似事情的人使用。
我的方法是改变 from 和 to 状态。
<div [@scrollAnimation]="isUp? fromState:toState"
(@scrollAnimation.done)="onAnimationEvent($event)" *ngFor="let question of
questions, let i = index">
<h2>{{i+1}}. {{ question.question }}</h2>
</div>
然后我会在单击上一个或下一个调用 toggle() 函数时动态更新问题索引。
toggle(direction) {
this.isUp = true;
this.direction = direction;
if(direction == 'up')
{
this.toState = "normal";
this.fromState = "down";
if(this.activeQuestion < this.questions.length-1)
this.activeQuestion++;
}
if(direction == 'down')
{
this.toState = "normal";
this.fromState = "up";
if(this.activeQuestion > 0)
this.activeQuestion--;
}
}
然后我将变量 isUp 设置为 true,这将调用动画,完成后它将调用更新状态的 onAnimationEvent() 函数。
onAnimationEvent ( event:AnimationEvent ) {
if(this.direction == "up")
{
this.fromState = "down";
this.toState ="normal";
}
if(this.direction == "down")
{
this.fromState = "up";
this.toState ="normal";
}
this.isUp = false;
this.show();
}
然后将 isUp 设置回 false 以播放下一个动画。我还在下面包含了我的动画代码。
trigger('scrollAnimation', [
state('up', style({transform: 'translateY(-20%)',opacity:0, offset: 0})),
state('normal', style({transform: 'translateY(20%)',opacity:1, offset: 0})),
state('down', style({transform: 'translateY(60%)',opacity:0, offset: 0})),
transition('normal => up', [
animate('0.5s ease')
]),
transition('normal => down', [
animate('0.5s ease')
]),
transition('up => normal', [
animate('2s ease', keyframes([
style({ transform: 'translateY(80%)',opacity:0, offset: 0.05 }),
style({ transform: 'translateY(20%)',opacity:1, offset: 0.55 }),
])),
]),
transition('down => normal', [
animate('2s ease', keyframes([
style({ transform: 'translateY(-20%)',opacity:0, offset: 0.05}),
style({ transform: 'translateY(20%)',opacity:1, offset: 0.55 }),
]))
]),
]),
我希望这会有所帮助。