gpt4 book ai didi

javascript - 如何通过名称获取动画

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:17 24 4
gpt4 key购买 nike

有没有办法通过名称获取动画样式规则?

我有这个动画:

@keyframes fadein {

0% {
opacity: 1;
}
37.5% {
opacity: 1;
}
50% {
opacity: 0;
}
87.5% {
opacity: 0;
}
100% {
opacity: 1;
}

}

我在多个地方都这样使用它:

#MyElement {
animation: fadein 5s linear 0s infinite;
pointer-events: none;
}

我想使用 JavaScript 将动画持续时间从 5 秒动态更改为 1 秒。

如果我想在不知道在哪里使用它的情况下更改动画持续时间,我该怎么做?

最佳答案

您可以像这样更改animation-duration:
myElement.style['animation-duration'] = value;

const myAnimatedElements = document.querySelectorAll(`.anim`)

const mySelect = document.querySelector(`select`)

mySelect.addEventListener(`change`, handleSelectChange)

function handleSelectChange(event) {
const value = event.target.value
myAnimatedElements.forEach(el => el.style[`animation-duration`] = value)
}
@keyframes fadein {
0% {
opacity: 1;
}
37.5% {
opacity: 1;
}
50% {
opacity: 0;
}
87.5% {
opacity: 0;
}
100% {
opacity: 1;
}
}

.anim {
animation: fadein 5s linear 0s infinite;
pointer-events: none;
}
<select>
<option value="5s">5s</option>
<option value="4s">4s</option>
<option value="3s">3s</option>
<option value="2s">2s</option>
<option value="1s">1s</option>
<option value="0.5s">0.5s</option>
</select>

<div class="anim">Animated</div>
<div class="anim">Animated</div>
<div class="anim">Animated</div>
<div class="anim">Animated</div>
<div class="anim">Animated</div>
<div class="anim">Animated</div>

关于javascript - 如何通过名称获取动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57340977/

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