Why this isn't working? What am I doing wrong?
CSS
@-webkit-keyframes test {
0% {
background-image: url('frame-01.png');
}
20% {
background-image: url('frame-02.png');
}
40% {
background-image: url('frame-03.png');
}
60% {
background-image: url('frame-04.png');
}
80% {
background-image: url('frame-05.png');
}
100% {
background-image: url('frame-06.png');
}
}
div {
float: left;
width: 200px;
height: 200px;
-webkit-animation-name: test;
-webkit-animation-duration: 10s;
-webkit-animation-iteration-count: 2;
-webkit-animation-direction: alternate;
-webkit-animation-timing-function: linear;
}
DEMO
Thanks in advance!
I needed to do the same thing recently. Here's a simple implementation
The
linear
timing function will animate the defined properties linearly. For the background-image it seems to have this fade/resize effect while changing the frames of you animation (not sure if it is standard behavior, I would go with @Chukie B's approach).If you use the
steps
function, it will animate discretely. See the timing function documentation on MDN for more detail. For you case, do like this:See this jsFiddle.
I'm not sure if it is standard behavior either, but when you say that there will be only one step, it allows you to change the starting point in the
@keyframes
section. This way you can define each frame of you animation.I needed to do the same thing as you and landed on your question. I ended up taking finding about the steps function which I read about from here.
JSFiddle of my solution in action (Note it currently works in Firefox, I'll let you add the crossbrowser lines, trying to keep the solution clean of clutter)
First I created a sprite sheet that had two frames. Then I created the div and put that as the background, but my div is only the size of my sprite (100px).
The animation is set to have 2 steps and have the whole process take 1 second.
Thiago above mentioned the steps function but I thought I'd elaborate more on it. Pretty simple and awesome stuff.
It works in Chrome 19.0.1084.41 beta!
So at some point in the future, keyframes could really be... frames!
You are living in the future ;)
You can follow by this code:
This is really fast and dirty, but it gets the job done: jsFiddle
I'm working on something similar for my site using jQuery, but the transition is triggered when the user scrolls down the page - jsFiddle