Below is the JavaScript code that I am using:
function toggleAnimation(x) {
if(!document.querySelector(".animate")){
x.classList.add("animate");
x.classList.remove("animater");
}else{
x.classList.remove("animate");
x.classList.add("animater");
}
The animation works fine on the first click, but subsequent clicks only show the end state without any animation. The "animater" class consists of three classes, all functioning properly. The
Here is the complete code snippet:
function toggleAnimation(x) {
if (!document.querySelector(".animate")) {
x.classList.add("animate");
x.classList.remove("animater");
} else {
x.classList.remove("animate");
x.classList.add("animater");
}
}
body {
background: #3FAF82;
text-align: center;
}
.line1,
.line2,
.line3 {
margin: 0px auto;
margin-top: 10px;
height: 5px;
width: 80px;
background-color: white;
border-radius: 5px;
box-shadow: 1px 2px 10px 0px rgba(0, 0, 0, 0.3);
transition: all ease 0.8s;
}
.lines {
margin-top: 50px;
cursor: pointer;
}
.animate .line3 {
animation: change3 ease 1 0.8s forwards;
}
.animater .line3 {
animation: change3 ease 1 0.8s forwards reverse;
}
.animate .line1 {
animation: change1 ease 1 0.8s forwards;
}
.animater .line1 {
animation: change1 ease 1 0.8s reverse forwards;
}
.animate .line2 {
width: 0px;
}
.animater .line2 {
width: 80px;
}
@keyframes change3 {
0% {
transform: translate3d(0, 0, 0) rotate(0deg);
}
50% {
transform: translate3d(0, -15px, 0) rotate(0deg);
}
100% {
transform: translate3d(0, -15px, 0) rotate(-45deg);
}
}
@keyframes change1 {
0% {
-webkit-transform: translate3d(0, 0, 0) rotate(0deg);
transform: translate3d(0, 0, 0) rotate(0deg);
}
50% {
-webkit-transform: translate3d(0, 15px, 0) rotate(0);
transform: translate3d(0, 15px, 0) rotate(0);
}
100% {
-webkit-transform: translate3d(0, 15px, 0) rotate(45deg);
transform: translate3d(0, 15px, 0) rotate(45deg);
}
}
<div class="lines" onclick="toggleAnimation(this)">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>