I am having an issue stopping a CSS animation at about 75% of the way complete and then reversing its order and then continuing it using JS. If you run the snippet below and you'll see that you can easily stop and reverse the animation of the cube and reverse it at 50%.
var div = document.getElementById('div');
var timer = setTimeout(function() {
div.classList.add('paused');
}, 1000)
var timer = setTimeout(function() {
div.classList.add('reverse');
div.classList.remove('paused');
}, 1700)
@keyframes animation {
0% {
margin-left: 0px;
}
100% {
margin-left: 200px;
}
}
#div {
width: 100px;
height: 100px;
background-color: red;
animation: animation 2s linear infinite;
}
.paused {
animation-play-state: paused !important;
}
.reverse {
animation-direction: reverse !important;
}
<div id="div"></div>
But, if you try to stop it at 75% and restart it backwards, it starts the paused animation at 25% (Snippet below).
var div = document.getElementById('div');
var timer = setTimeout(function() {
div.classList.add('paused');
}, 1500)
var timer = setTimeout(function() {
div.classList.add('reverse');
div.classList.remove('paused');
}, 1700)
var timer = setTimeout(function() {
div.classList.add('paused');
}, 2800)
@keyframes animation {
0% {
margin-left: 0px;
}
100% {
margin-left: 200px;
}
}
#div {
width: 100px;
height: 100px;
background-color: red;
animation: animation 2s linear infinite;
}
.paused {
animation-play-state: paused !important;
}
.reverse {
animation-direction: reverse !important;
}
<div id="div"></div>
So, my question is, is there a way around this? Some sort of CSS property. I know that this may not be considered a glitch, because at 75% complete on reverse, the box should be at 25%, even though it moves across the screen much too quickly.