В настоящее время у меня есть этот код в 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>
Я бы использовал переменные CSS... в основном потому, что они классные и простые в использовании.
<style>
:root {
--li-transform-animation-duration: 60s;
}
ul li.transform a {
color: red;
-webkit-animation-name: transform;
-webkit-animation-duration: var(-li-transform-animation-duration);
-webkit-animation-fill-mode: forwards;
animation-name: transform;
animation-duration: var(-li-transform-animation-duration);
animation-fill-mode: forwards;
}
</style>
<script>
let root = document.documentElement;
root.style.setProperty('--li-transform-animation-duration', '40s');
</script>
Как насчет использования переменных CSS. Переменные можно изменить с помощью JS.
const transforms = document.getElementsByClassName('transform');
transforms[3].querySelector('a').style.setProperty('--transform-duration', '500ms')
ul li.transform a {
--transform-duration: 60s; /* Default time */
color: red;
-webkit-animation-name: transform;
-webkit-animation-duration: var(--transform-duration);
-webkit-animation-fill-mode: forwards;
animation-name: transform;
animation-duration: var(--transform-duration);
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>
<li class = "transform">
<a href = "#" style = "--transform-duration:5s">link1</a>
</li>
<li class = "transform">
<a href = "#" style = "--transform-duration:1s">link2</a>
</li>
<li class = "transform">
<a href = "#">link3 is 60s but reset to .5s</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>
связанные: stackoverflow.com/q/49750473/8620333