Scrub CSS @keyframes
So you want to scrub a CSS @keyframes animation back and forth but there's no CSS property to set the current time.
Breath. Don't try to change the current time, change the animation. Imagine a timeline Daniel San. An animation floats on this timeline. The current time is paused and rests on the timeline. If you change the animation-delay property, the animation moves back and forth but the current time still does not move.
Here's some code:
<style>
#input {
width: 300px;
}
#sponge {
width: 100px;
height: 140px;
border-radius: 8px;
margin: 40px 0;
background-color: #fff;
}
@keyframes scrub {
from {
transform: translate3d(10px, 0, 0) rotate(-15deg);
}
to {
transform: translate3d(300px, 0, 0) rotate(15deg);
}
}
#sponge {
animation-duration: 1s;
animation-play-state: paused;
animation-fill-mode: both;
animation-timing-function: linear;
animation-name: scrub;
}
</style>
<div id="sponge"></div>
<input id="input" type="range" value="0" min="0" max="1" step="0.01"/>
<script>
document.getElementById('input').addEventListener('input', e => {
let currentTime = e.target.value;
document.getElementById('sponge').style.animationDelay = `-${currentTime}s`;
});
</script>
See it in action.