【发布时间】:2021-12-03 16:55:50
【问题描述】:
我希望 div 框的颜色和大小在单击按钮时具有动画效果并返回其原始值。这是我的代码示例:
document.getElementById("andAction").addEventListener("click", function() {
document.getElementById("box").classList.toggle("animi");
})
.thing {
transform: translate(150px, 100px);
}
.box {
background-color: #999;
padding: 2px;
color: black;
width:20px;
margin: 0 auto;
text-align: center;
color: #fff;
}
@keyframes blob {
0% {
background-color: #999;
}
50% {
background-color: #F9086D;
transform: scale(2);
background-color: red;
border-radius: 20px;
}
100% {
background-color: #999;
}
}
.animi {
animation-name: blob;
animation-duration:3s;
animation-iteration-count:1;
}
<button id="andAction" class="button">button</button>
<div id="box" class="box">1</div>
问题
我的问题是我正在使用切换。这意味着我必须第二次点击两次。另一个品种是classList.add,然后再次删除。这导致没有结果,因为没有为用户启动动画。我唯一能做的就是使用超时。
问题
我感觉还有别的办法?
【问题讨论】:
标签: javascript css animation css-animations