【发布时间】:2022-01-29 07:00:52
【问题描述】:
我正在尝试在以下内容上堆叠 CSS 关键帧(动画延迟),但我不确定如何以编程方式执行此操作?
.r1 {
animation-name: move1;
animation-delay: 0.5s;
animation-duration: 1s;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}
.r2 {
animation-name: move1;
animation-delay: 1.5s;
animation-duration: 1s;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}
.r3 {
animation-name: move1;
animation-delay: 2.5s;
animation-duration: 1s;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}
@keyframes move1 {
to {
transform: translateX(200px);
}
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect class="r1" x="10" y="20" width="100" height="100" fill="red"/>
<rect class="r2" x="10" y="130" width="100" height="100" fill="green"/>
<rect class="r3" x="10" y="240" width="100" height="100" fill="blue"/>
</svg>
animation-duration 是硬编码的每个类,animation-delay 是硬编码的第一类,即r1。
如何传递r2和r3的延迟,比如
r2 delay= r1 delay + r1 duration->0.5+1=1.5s
和
r3 delay= r2 delay + r2 duration ->1.5+1=2.5s
在 javascript 中有什么东西可以按类给出animation-duration 吗?
我尝试通过 Element.getAnimations() 执行此操作,但我不确定是否有任何东西可以按类提供动画持续时间。
我不想手动执行此操作,因为我在 svg 中有很多类。
提前谢谢你。
【问题讨论】:
-
calc() 是否适用于您的情况?
-
如果您有 calc 解决方案,我可以尝试告诉您
-
如何在当前类中为上一类获取动画持续时间
标签: javascript css svg css-animations