I am seeking a solution to animate the color and size of a div box, then return it to its original state when a button is clicked. Here is an example of my code:
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>
Issue
The problem I encountered was with using toggle, requiring two clicks for the desired effect. I also tried classList.add
followed by remove, but this did not initiate the animation. Using timeout was one workaround.
Query
I believe there might be another approach to achieve this. Any suggestions?