gpt4 book ai didi

javascript - 如何以编程方式知道哪个是最后一个具有动画的对象

转载 作者:行者123 更新时间:2023-12-05 00:36:16 25 4
gpt4 key购买 nike

有没有办法让我以编程方式知道 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是动画应用的最后一个元素。有什么方法可以检测到 animation-delay+animation-duration对于 r3 - 使用 javascript 应用动画的最后一个元素。

最佳答案

Web Animations API可以检查动画。document.getAnimations()返回在文档中找到的所有动画。他们每个人都有一个 effect属性,在您的情况下,它们都是 KeyframeEffect 类型.

  • animation.animationName返回 CSS @keyframes 中所述的动画名称宣言。
  • animation.effect.target返回动画所针对的元素。
  • animation.effect.getComputedTiming().endTime返回动画结束的时间。

  • 从那里您可以比较和过滤您需要的信息。

    document.getAnimations().forEach(animation => {
    console.log(
    animation.animationName,
    animation.effect.target.id,
    animation.effect.getComputedTiming().endTime
    );
    });
    .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>

    关于javascript - 如何以编程方式知道哪个是最后一个具有动画的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70991093/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com