I have code as below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 100px; height: 100px;
background-color: red;
}
div.forward {
animation-name: demo;
animation-direction: normal;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-timing-function: ease;
}
div.backward {
animation-name: demo;
animation-direction: reverse;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-timing-function: ease;
}
@keyframes demo {
0% {
transform: translateX(0);
}
100% {
transform: translateX(100px);
}
}
</style>
</head>
<body>
<body>
<script>
function t(div) {
div.classList.remove("backward");
div.classList.remove("forward");
}
function forward(div) {
div.classList.add("forward");
}
function backward(div) {
div.classList.add("backward");
}
var b = true;
function f() {
var div = document.getElementsByTagName("div")[0];
t(div);
if (b) {
forward(div);
}
else {
backward(div);
}
b = !b;
}
</script>
<div onclick="f()"></div>
</body>
</body>
</html>
with the purpose of running the div element back and forth by changing animation-direction from normal to reverse and vice versa. When I first click the div element the animation runs fine, the seccond and further the animation just immidiately run back and forth without any animations but when I run the code line by line in chrome DevTool
this works as I expected and I don't know why :(