I have a problem regarding the css animation triggering with JavaScript. This is my code snippet:
const signInBtn = document.querySelector('#signin-btn');
function btnAnimation() {
if (signInBtn.style.animation) {
signInBtn.style.animation = '';
} else {
signInBtn.style.animation = 'btnAnimation 200ms linear';
}
}
signInBtn.addEventListener('click', btnAnimation);
body {
width: 100%;
height: 100vh;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
button {
border: 2px solid black;
border-radius: 25px;
width: 100px;
height: 40px;
}
button:focus {
outline: none;
}
.wrapper .signin {
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
@keyframes btnAnimation {
0% {
transform: scale(1);
}
50% {
transform: scale(.8);
}
100% {
transform: scale(1);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<div class="signin">
<button id="signin-btn">SIGN IN</button>
</div>
</div>
</body>
</html>
So as you can see animation triggers every second click. I can't figure out how to add animation property after i removed it in "if" statement. I need this button to animate in every click. It is ok if you have a solution with jQuery but i would like to see pure JS solution. Thanks in advance.