gpt4 book ai didi

css - 将参数传递给 css 动画

转载 作者:技术小花猫 更新时间:2023-10-29 11:29:17 27 4
gpt4 key购买 nike

p {
animation-duration: 3s;
animation-name: slidein;
}

@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
}

to {
margin-left: 0%;
width: 100%;
}
}

对于上面的动画 CSS 代码,我想知道是否有任何方法可以将 margin-leftwidth 的值作为参数从 Javascript 传递。

最佳答案

使用 CSS 变量,您可以轻松做到这一点:

document.querySelector('.p2').style.setProperty('--m','100%');
document.querySelector('.p2').style.setProperty('--w','300%');
.p1,.p2 {
animation-duration: 3s;
animation-name: slidein;
}

@keyframes slidein {
from {
margin-left: var(--m, 0%);
width: var(--w, 100%);
}
to {
margin-left: 0%;
width: 100%;
}
}
<p class="p1">
This will not animate as the animation will use the default value set to the variable
</p>
<p class="p2">
This will animate because we changed the CSS variable using JS
</p>

关于css - 将参数传递给 css 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49750473/

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