我現(xiàn)在正在做谷歌恐龍項目,我想做一個隨機仙人掌動畫時間和延遲,但我不知道如何,這里是我的代碼:
`.cactus-anim {
animation: cactus-move 1.3s 0.5s linear infinite;
}
@keyframes cactus-move {
0% {
left: 700px;
}
100% {
left: -40px;
}
}`
(我想用1.3秒來代替隨機(1-2)秒,用0.5秒來代替隨機(0-0.7)秒,我也用Scss和Js,Thnks
您可以將這段代碼添加到JS中,并在需要隨機化動畫時間時運行它,但這在普通CSS中是不可能的。
const anim = document.querySelector(".cactus-anim");
// gets a random number between 2 values
function randNum(min, max) {
return Math.random() * (max - min) + min
}
// run this function to set a new, random animation time
function randAnimTime() {
// sets the new animation time
// first time random is between 1 and 2 seconds
// second is random between 0 and 0.7 seconds
anim.style.animation = "cactus-move " + randNum(1, 2) + "s " + randNum(0, 0.7) + "s linear infinite";
}
上一篇vue id和ref