【发布时间】:2022-02-05 09:19:23
【问题描述】:
有没有办法让我以编程方式知道 CSS 动画最后应用在哪个对象上
例如,
.r1 {
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;
}
.c1 {
animation-name: blink;
animation-delay: 0.5s;
animation-duration: 1s;
animation-iteration-count: 2;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}
/*.text1 {
animation-name: scl;
animation-delay: 5.5s;
animation-duration: 1s;
animation-iteration-count: 2;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}*/
.r2 {
transform-origin: center;
transform-box: fill-box;
animation-name: gr;
animation-delay: 3.5s;
animation-duration: 2s;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-direction: normal;
animation-fill-mode: forwards;
}
.r3 {
animation-name: move2;
animation-delay: 7.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);
}
}
@keyframes blink {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes gr {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
@keyframes scl {
to {
transform: scale(1.1);
}
}
@keyframes move2 {
to {
transform: translateY(400px);
}
}
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect id="r1" class="r1" x="10" y="20" width="100" height="100" fill="red" />
<rect id="r2" class="r2" x="10" y="130" width="100" height="100" fill="green" />
<rect id="r3" class="r3" x="10" y="240" width="100" height="100" fill="blue" />
<circle id="c1" class="c1" cx="50" cy="400" r="40" fill="orange" />
<text class="text1" id="text1" x="80" y="500" font-size="30" fill="red">I love SVG!</text>
</svg>
在此,我有 5 个元素,我在其中 4 个元素上应用动画,r3 是应用动画的最后一个元素。有没有办法我可以检测到r3 的animation-delay+animation-duration - 最后一个使用javascript 应用动画的元素。
【问题讨论】:
-
我不太确定你在追求什么:在最后一个动画完成时收到通知,并确定它是哪一个,或者识别,独立于实际运行的动画,哪个将最后完成,或者获得
.r3上的动画什么时候结束(因为你提前知道它会是最后一个)? -
您能否更详细地描述“检测”的含义。这只是 JS 中查找动画延迟和动画持续时间设置的一些代码,可以完成,还是以某种方式测量页面加载和 r3 上动画结束之间的时间,这不能完成(嗯,不完全准确,因为我们不在实时系统环境中)。
标签: javascript css svg css-animations