Qia*_*hen 6 css css3 keyframe css-animations
p {
animation-duration: 3s;
animation-name: slidein;
}
@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
}
to {
margin-left: 0%;
width: 100%;
}
}
Run Code Online (Sandbox Code Playgroud)
对于上面的动画CSS代码,我想知道是否有任何方法可以从Javascript 传递margin-left
和width
作为参数的值.
Tem*_*fif 10
使用CSS变量,您可以轻松地执行此操作:
document.querySelector('.p2').style.setProperty('--m','100%');
document.querySelector('.p2').style.setProperty('--w','300%');
Run Code Online (Sandbox Code Playgroud)
.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%;
}
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)