I have a sprite that contains various frames of an animation. I want to have control over it, pausing at particular frames using javascript.
I could put all the images into different files and the animations in a gif… but this seems overkill. I’d like to use CSS animations to animate background-position. However, currently it will animate over time and the background will slide. Is there any way to use CSS animations that will just set the values from keyframe1 e.g. position 0,0 then set the values from keyframe2 position 0,200. If I try this, it currently “slides” across during the duration of the element. What I want to happen is:
Display keyframe 1
Wait 0.1s
Display keyframe 2
wait 0.1s
Display keyframe 2
Is it possible to do this without animating the property so the transition between frames is instant rather than animated, but the delay between the frames still exists?
Or will I have to compile all the possible permutations of the animation into different gifs?
However, if you wanted to do the math (find out how many frames, etc)…then you could do something like I cooked up here. If you follow the math in this CSS, it’s 1 secod per slide. Should be a good start.
The image will change between 24.9% and 25% (rather than 0% - 25%) so the change is very quick. It does depend on the overall time of the animation but I still think it will be instant.
Here’s a codepen (although codepen seems to be crashing at the moment due to server load).
There is a tutorial here on css sprite animation. It uses steps(#) to set the number of frames within the duration, which will stop the sliding and save you adding a bg-pos for every frame.