gpt4 book ai didi

javascript - 如何根据 javascript 值使动画持续时间动态化

转载 作者:行者123 更新时间:2023-11-29 18:43:11 25 4
gpt4 key购买 nike

我目前在 css 中有这段代码,它从一种颜色转换为另一种颜色,但我想根据 javascript 中的某些值使持续时间动态变化,而不是让它成为固定值。

我必须采取哪些解决方案才能实现它?这是我当前转换它的 CSS

ul li.transform a { 
color: red;
-webkit-animation-name: transform;
-webkit-animation-duration: 60s;
-webkit-animation-fill-mode: forwards;
animation-name: transform;
animation-duration: 60s;
animation-fill-mode: forwards;
}

@-webkit-keyframes transform{
from { color: red; }
to { color: green; }
}

@keyframes transform {
from { color: red; }
to { color: green; }
}
<ul>
<li class="transform">
<a href="#">link</a>
</li>
</ul>

最佳答案

您还可以使用 animationDuration 属性来定义动画持续时间。

(function() {
let time = "3s",
set_duration = () => {
document.getElementById("link").style.WebkitAnimationDuration = time;
document.getElementById("link").style.animationDuration = time;
};

if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
set_duration();
} else {
document.addEventListener('DOMContentLoaded', set_duration);
}
})();
ul li.transform a { 
color: red;
-webkit-animation-name: transform;
-webkit-animation-fill-mode: forwards;
animation-name: transform;
animation-fill-mode: forwards;
}

@-webkit-keyframes transform {
from { color: red; }
to { color: green; }
}

@keyframes transform {
from { color: red; }
to { color: green; }
}
<ul>
<li class="transform">
<a href="#" id="link">link</a>
</li>
</ul>

关于javascript - 如何根据 javascript 值使动画持续时间动态化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55618416/

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