【发布时间】:2021-04-18 11:19:51
【问题描述】:
我使用 Angular Animations 创建了以下轮播:
我创建了 5 个变量来定义每个状态的位置:
但是当我尝试添加更多数据时,一些项目会从动画中消失(如上图中的“Joe”和“Hidde”)。
我正在努力实现以下目标:
(i) 如果数据更多,我需要继续播放动画(从右侧无处进入 DOM,我想我们可以使用 ':enter' 和 ':leave' 别名来做到这一点,但不能在这里应用它们。)
(ii) 假设如果只有两个项目,那么项目应该在“当前”和“下一个”状态之间进行动画处理,但不会转到最右边。
这是 Stackblitz link。
这是代码:
.ts:
animations: [
trigger("carousel", [
state(
"current",
style({
position: "absolute",
top: "16%",
right: "54%",
bottom: "39%",
left: "40%"
})
),
state(
"next",
style({
position: "absolute",
top: "51%",
right: "77%",
bottom: "calc(2% + 3px)",
left: "calc(1% + 6px)"
})
),
state(
"futureNext",
style({
position: "absolute",
top: "51%",
right: "54%",
bottom: "calc(2% + 3px)",
left: "calc(26% + 5px)"
})
),
state(
"postFutureNext",
style({
position: "absolute",
top: "51%",
right: "31%",
bottom: "calc(2% + 3px)",
left: "calc(51% + 5px)"
})
),
state(
"nextToPostFutureNext",
style({
position: "absolute",
top: "51%",
right: "8%",
bottom: "calc(2% + 3px)",
left: "calc(76% + 4px)"
})
),
transition(
"current <=> nextToPostFutureNext",
animate("2000ms ease-in-out")
),
transition(
"nextToPostFutureNext <=> postFutureNext",
animate("2000ms ease-in-out")
),
transition(
"postFutureNext <=> futureNext",
animate("2000ms ease-in-out")
),
transition("futureNext <=> next", animate("2000ms ease-in-out")),
transition("next <=> current", animate("2000ms ease-in-out"))
])
]
clearTimeOutVar: any;
current = 0;
next = 1;
futureNext = 2;
postFutureNext = 3;
nextToPostFutureNext = 4;
ngOnInit() {
this.runSlideShow();
}
runSlideShow() {
this.clearTimeOutVar = setTimeout(() => {
this.updateAnimationVariables();
this.runSlideShow();
}, 3000);
}
updateAnimationVariables() {
if (this.current === 4) {
this.current = 0;
} else if (this.current < 4) {
this.current++;
}
if (this.next === 4) {
this.next = 0;
} else if (this.next < 4) {
this.next++;
}
if (this.futureNext === 4) {
this.futureNext = 0;
} else if (this.futureNext < 4) {
this.futureNext++;
}
if (this.postFutureNext === 4) {
this.postFutureNext = 0;
} else if (this.postFutureNext < 4) {
this.postFutureNext++;
}
if (this.nextToPostFutureNext === 4) {
this.nextToPostFutureNext = 0;
} else if (this.nextToPostFutureNext < 4) {
this.nextToPostFutureNext++;
}
}
ngOnDestroy() {
clearTimeout(this.clearTimeOutVar);
}
.html:
<div class="container">
<div class="header">
A Simple Carousel
</div>
<div class="data">
<div class="cards" *ngFor="let item of data; let i = index;" [@carousel]="
i == current ? 'current' :
i == next ? 'next' :
i == futureNext ? 'futureNext' :
i == postFutureNext ? 'postFutureNext' :
i == nextToPostFutureNext ? 'nextToPostFutureNext' : ''
">
<div class="name">{{ item.name }}</div>
<div class="age">{{ item.age }}</div>
</div>
</div>
</div>
如果我无法解释我的方法中的任何一点,请告诉我。
谢谢。
【问题讨论】:
标签: angular angular-animations