CSS animation in different browsers [duplicate] - html

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
http://jsfiddle.net/hAGKv/

Updated for 2020: Yes, it can be done! Here's how.
Snippet demo:
#mydiv{ animation: changeBg 1s infinite; width:143px; height:100px; }
#keyframes changeBg{
0%,100% {background-image: url("https://i.stack.imgur.com/YdrqG.png");}
25% {background-image: url("https://i.stack.imgur.com/2wKWi.png");}
50% {background-image: url("https://i.stack.imgur.com/HobHO.png");}
75% {background-image: url("https://i.stack.imgur.com/3hiHO.png");}
}
<div id='mydiv'></div>
Background image [isn't a property that can be animated][1] - you can't tween the property.
Original Answer: (still a good alternative)
Instead, try laying out all the images on top of each other using position:absolute, then animate the opacity of all of them to 0 except the one you want repeatedly.

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 ;)

Works for me.
Notice the use of background-image for transition.
#poster-img {
background-repeat: no-repeat;
background-position: center;
position: absolute;
overflow: hidden;
-webkit-transition: background-image 1s ease-in-out;
transition: background-image 1s ease-in-out;
}

This is really fast and dirty, but it gets the job done: jsFiddle
#img1, #img2, #img3, #img4 {
width:100%;
height:100%;
position:fixed;
z-index:-1;
animation-name: test;
animation-duration: 5s;
opacity:0;
}
#img2 {
animation-delay:5s;
-webkit-animation-delay:5s
}
#img3 {
animation-delay:10s;
-webkit-animation-delay:10s
}
#img4 {
animation-delay:15s;
-webkit-animation-delay:15s
}
#-webkit-keyframes test {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
}
}
#keyframes test {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
}
}
I'm working on something similar for my site using jQuery, but the transition is triggered when the user scrolls down the page - jsFiddle

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).
<div id="cyclist"></div>
#cyclist {
animation: cyclist 1s infinite steps(2);
display: block;
width: 100px;
height: 100px;
background-image: url('../images/cyclist-test.png');
background-repeat: no-repeat;
background-position: top left;
}
The animation is set to have 2 steps and have the whole process take 1 second.
#keyframes cyclist {
0% {
background-position: 0 0;
}
100% {
background-position: 0 -202px; //this should be cleaned up, my sprite sheet is 202px by accident, it should be 200px
}
}
Thiago above mentioned the steps function but I thought I'd elaborate more on it. Pretty simple and awesome stuff.

Your code can work well with some adaptations :
div {
background-position: 50% 100%;
background-repeat: no-repeat;
background-size: contain;
animation: animateSectionBackground infinite 240s;
}
#keyframes animateSectionBackground {
00%, 11% { background-image: url(/assets/images/bg-1.jpg); }
12%, 24% { background-image: url(/assets/images/bg-2.jpg); }
25%, 36% { background-image: url(/assets/images/bg-3.jpg); }
37%, 49% { background-image: url(/assets/images/bg-4.jpg); }
50%, 61% { background-image: url(/assets/images/bg-5.jpg); }
62%, 74% { background-image: url(/assets/images/bg-6.jpg); }
75%, 86% { background-image: url(/assets/images/bg-7.jpg); }
87%, 99% { background-image: url(/assets/images/bg-8.jpg); }
}
Here is the explanation of the percentage to suit your situation:
First you need to calculate the "chunks". If you had 8 differents background, you need to do :
100% / 8 = 12.5% (to simplify you can let fall the decimals) => 12%
After that you obtain that :
#keyframes animateSectionBackground {
00% { background-image: url(/assets/images/bg-1.jpg); }
12% { background-image: url(/assets/images/bg-2.jpg); }
25% { background-image: url(/assets/images/bg-3.jpg); }
37% { background-image: url(/assets/images/bg-4.jpg); }
50% { background-image: url(/assets/images/bg-5.jpg); }
62% { background-image: url(/assets/images/bg-6.jpg); }
75% { background-image: url(/assets/images/bg-7.jpg); }
87% { background-image: url(/assets/images/bg-8.jpg); }
}
If you execute this code, you will see the transition will be permanantly. If you want the backgrounds stay fixed while a moment, you can do like this :
#keyframes animateSectionBackground {
00%, 11% { background-image: url(/assets/images/bg-1.jpg); }
12%, 24% { background-image: url(/assets/images/bg-2.jpg); }
25%, 36% { background-image: url(/assets/images/bg-3.jpg); }
37%, 49% { background-image: url(/assets/images/bg-4.jpg); }
50%, 61% { background-image: url(/assets/images/bg-5.jpg); }
62%, 74% { background-image: url(/assets/images/bg-6.jpg); }
75%, 86% { background-image: url(/assets/images/bg-7.jpg); }
87%, 99% { background-image: url(/assets/images/bg-8.jpg); }
}
That mean you want :
bg-1 stay fixed from 00% to 11%
bg-2 stay fixed from 12% to 24%
etc
By putting 11%, the transtion duration will be 1% (12% - 11% = 1%).
1% of 240s (total duration) => 2.4 seconds.
You can adapt according to your needs.

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:
-webkit-animation-timing-function: steps(1,end);
animation-timing-function: steps(1,end);
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.

Like the above stated, you can't change the background images in the animation. I've found the best solution to be to put your images into one sprite sheet, and then animate by changing the background position, but if you're building for mobile, your sprite sheets are limited to less than 1900x1900 px.

I needed to do the same thing recently. Here's a simple implementation
#wrapper { width:100%; height:100%; position:relative; }
#wrapper img { position:absolute; top:0; left:0; width:100%; height:auto; display:block; }
#wrapper .top { animation:fadeOut 2s ease-in-out; animation-fill-mode:forwards; }
#keyframes fadeOut {
0% { opacity:1; }
100% { opacity:0; }
}
<div id="wrapper">
<img src="img1.jpg" class="top" style="z-index:2;">
<img src="img2.jpg" style="z-index:1;">
</div>

You can use animated background-position property and sprite image.

You can follow by this code:
#cd{
position: relative;
margin: 0 auto;
height: 281px;
width: 450px;
}
#cf img{
left: 0;
position: absolute;
-moz-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf img.top:hover{
opacity: 0;
}
<div id="cf">
<img class="button" src="Birdman.jpg" />
<img src="Turtle.jpg" class="top" />
</div>

You can use the jquery-backstretch image which allows for animated slideshows as your background-images!
https://github.com/jquery-backstretch/jquery-backstretch
Scroll down to setup and all of the documentation is there.

Well I can change them in chrome. Its simple and works fine in Chrome using -webkit css properties.

Related

Only with CSS, looping 30 images one after another for infinite time creating a burning fire effect

sorry if this question repeats another one , but i did not find result for only CSS usage for creating this effect .
I have 30 images , with which i need to create a burning effect ( hence they need to show in the same div content and to loop infinite.
I tried with percentages and with adding the browser prefixes as well. Also with "opacity property and "fade in" , but i get only one image to repeat itself.
.box4 {
grid-column: 2/3;
grid-row: 2/3;
}
.box4 img {
display: flex;
flex-direction: row;
align-items: center;
width: 60%;
height: 80px;
margin-left: 20%;
margin-top: 5%;
animation-name: myAnimation;
animation-duration: 30s;
animation-iteration-count: infinite;
}
#keyframes myAnimation {
0% {
background-image: url(../fireImages/fire_1.png)
}
5% {
background-image: url(../fireImages/fire_2.png)
}
10% {
background-image: url(../fireImages/fire_3.png)
}
15% {
background-image: url(../fireImages/fire_4.png)
}
20% {
background-image: url(../fireImages/fire_5.png)
}
28% {
background-image: url(../fireImages/fire_6.png)
}
32% {
background-image: url(../fireImages/fire_7.png)
}
36% {
background-image: url(../fireImages/fire_8.png)
}
40% {
background-image: url(../fireImages/fire_9.png)
}
45% {
background-image: url(../fireImages/fire_10.png)
}
49% {
background-image: url(../fireImages/fire_11.png)
}
53% {
background-image: url(../fireImages/fire_12.png)
}
58% {
background-image: url(../fireImages/fire_13.png)
}
62% {
background-image: url(../fireImages/fire_14.png)
}
64% {
background-image: url(../fireImages/fire_15.png)
}
68% {
background-image: url(../fireImages/fire_16.png)
}
72% {
background-image: url(../fireImages/fire_17.png)
}
76% {
background-image: url(../fireImages/fire_18.png)
}
78% {
background-image: url(../fireImages/fire_19.png)
}
82% {
background-image: url(../fireImages/fire_20.png)
}
84% {
background-image: url(../fireImages/fire_21.png)
}
86% {
background-image: url(../fireImages/fire_22.png)
}
88% {
background-image: url(../fireImages/fire_23.png)
}
90% {
background-image: url(../fireImages/fire_24.png)
}
92% {
background-image: url(../fireImages/fire_25.png)
}
94% {
background-image: url(../fireImages/fire_26.png)
}
96% {
background-image: url(../fireImages/fire_27.png)
}
98% {
background-image: url(../fireImages/fire_28.png)
}
99% {
background-image: url(../fireImages/fire_29.png)
}
100% {
background-image: url(../fireImages/fire_30.png)
}
}
<div class="box4"><img src="assets/fireImages/fire_1.png"></div>
I have also tried with creating css class , so the image src not to be the URL , but the class, and still does not work. i am doing ,i guess, everything wrong , but i need somoone to point me in a direction .
Thank you to everyone in advance
Unfortunately, the background image isn't a property that can be animated via CSS. For that effect, you'll have no other choice just to use a script. Reference : https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
Your mistake is to use the background-image property on an img element. You can have background-image on am img element, but in that case the background image will be hidden by the actual image, unless it has transparent parts, which it does not in your case. Dispense with the img element and use background-image on a regular div.
Kornelijus is throwing a bit of a curveball when he says that background-image can't be animated. That just means that it can't be changed gradually, as it says on the page he links to. The concept of changing a background image gradually doesn't make sense anyway - it is either one image or another.
Run the code snippet below to see the concept in action. I have reduced the number of keyframes, and used inlined images so that you can't actually see the images changing.
.box4 {
grid-column:2/3;
grid-row:2/3;
}
.box4{
display:flex;
flex-direction: row;
align-items: center;
width: 315px;
height: 65px;
margin-left: 20%;
margin-top: 5%;
animation-name:myAnimation;
animation-duration:2s;
animation-iteration-count: infinite;
}
#keyframes myAnimation {
0%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigwLDAsMjU1KTsiIC8+PC9zdmc+")}
20%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigyNTUsMCwwKTsiIC8+PC9zdmc+")}
40%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigwLDAsMjU1KTsiIC8+PC9zdmc+")}
60%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigwLDAsMjU1KTsiIC8+PC9zdmc+")}
80%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigyNTUsMCwwKTsiIC8+PC9zdmc+")}
100%{ background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzE1IiBoZWlnaHQ9IjY1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIzMTUiIGhlaWdodD0iNjUiIHN0eWxlPSJmaWxsOnJnYigwLDAsMjU1KTsiIC8+PC9zdmc+")}
}
<div class="box4"></div>
I would not recommend doing an animation this way though. A video would be better. Images are not requested until they are first rendered on the page, so if you are referencing external images then they will not have been downloaded in time for the first loop of the animation.
Thank you both.
The case is that this is a task with requirements is to make an animation with the 30 images i got , i can also use Sass/Less/Stylus , but i am newly interested in programming and have not yet made a research on those CSS Preprocessors ( can the animation be made with the preporcessors? ) . Since there are no specific guidance , i can just make a gift out of those image and assign it to the div

How to smooth out a CSS gradient transition?

I am creating an interactive touchscreen display using a program called Intuiface and have created some background tiles/squares that I want to make look 'alive' by transitioning slowly between colours.
I have used a linear-gradient transition in CSS to do it but the problem is that the transition looks choppy. The program is running 12 visible tiles (it is a very large touchscreen).
I have tried using fewer colours and running on more powerful GPUs (I think it is CPU run anyway) but this hasn't helped.
body {
width: 100wh;
height: 90vh;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
background-size: 400% 400%;
animation: Gradient 15s ease infinite;
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
At the moment the animations are noticeably choppy. I would like the transition to be much smoother. Does anyone know how I can achieve this?
Here is the code snippet.
body {
width: 100wh;
height: 90vh;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
background-size: 400% 400%;
animation: Gradient 15s ease infinite;
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
<html>
<body>
</body>
</html>
Animating background-* properties can be resource intensive - you can try animating transform for relatively better performance - see demo below using traslate for the animation:
body {
margin: 0;
}
div {
height: 100vh;
overflow: hidden;
}
div:after {
content: '';
display: block;
width: 400vw;
height: 400vh;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
animation: gradient 15s ease infinite;
}
#keyframes gradient {
50% {
transform: translate(-300vw, -300vh);
}
}
<div></div>
Since your animation lasts 15 seconds, trying to run it at full 60fps would mean calculating 15*60 = 900 frames.
Since the difference between a frame and the next is quite small, you can make the CPU work quite less asking for a stepped animation, for instance with steps(75)
It could be also good to set slight delays between animations, so that they don't execute at the same time
body {
width: 100wh;
height: 90vh;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
background-size: 400% 400%;
animation: Gradient 15s infinite steps(75);
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
<html>
<body>
</body>
</html>

Crossfading the background-image of a single element without script

I want to switch background image of the body periodically, cross fading between each image.
A script solution would look like this:
css:
body
{
background-image: url("img/1.jpg");
background-size: cover;
background-position: center 0;
background-attachment: fixed;
transition: background-image 2s ease-in-out;
}
js:
var images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg"];
var current_image = 0;
$(function ()
{
var body = $("body");
setTimeout(next, 10000);
function next()
{
current_image = (current_image + 1) % images.length;
body.css("background-image", "url('img/" + images[current_image] + "')");
setTimeout(next, 10000);
}
});
But is it possible to cross fade the background of a single element (as opposed to change opacities of a number of img elements) using no scripting?
Yeah you could do it with css animations.
something like this.
* { box-sizing: border-box}
.slides {
width: 300px;
height: 300px;
background: tomato;
animation: images 4s linear 0s infinite alternate;
}
#keyframes images {
0% {
background: url('https://fillmurray.com/300/300')
}
50% {
background: url('http://www.placecage.com/c/300/300');
}
100% {
background: url('https://stevensegallery.com/300/300')
}
}
<div class="slides"></div>
I post this as an answer to my own question even if there is a solution that I accepted. What this will add is how to stay for a while on the same image without immediately transitioning to the next.
body {
background-size: cover;
background-position: center 0;
background-attachment: fixed;
animation: images 100s linear 0s infinite;
}
#keyframes images {
0% {
background-image: url("img/1.jpg")
}
19% {
background-image: url("img/1.jpg")
}
20% {
background-image: url("img/2.jpg");
}
39% {
background-image: url("img/2.jpg");
}
40% {
background-image: url("img/3.jpg");
}
59% {
background-image: url("img/3.jpg");
}
60% {
background-image: url("img/4.jpg");
}
79% {
background-image: url("img/4.jpg");
}
80% {
background-image: url("img/5.jpg");
}
99% {
background-image: url("img/5.jpg");
}
100% {
background-image: url("img/1.jpg")
}
}
I tried to group some percentages like
0%, 19%, 100% {
background-image: url("img/1.jpg");
}
but that resulted in "flickering" between images.

CSS3 animations too fast and background image shaky a little bit

After executing this, background changes too fast and also it is a bit shaking. Help me to slow down this background change and stop background to shake.
HTML
<!-- Banner -->
<section id="banner">
<div class="inner">
<h2>Enhancing Your <br />Ways</h2>
<p>A free platform for schedualing</p>
</div>
</section>
CSS (Animation Delays not working)
<!--The animation-delays not working-->
#keyframes changebackground {
0% {
background-image: url("../Images/4.jpg");
animation-delay:5s;
}
25% {
background-image: url("../Images/1.jpg") ;
animation-delay:5s;
}
50% {
background-image: url("../Images/2.jpg") ;
animation-delay:5s;
}
100% {
background-image: url("../Images/3.jpg");
animation-delay:5s;
}
}
#banner {
margin-top:2.9em;
background-image: url("../Images/4.jpg");
background-position:center;
background-repeat:no-repeat;
padding:22em 0em 8em 0em;
background-size:cover;
width:100%;
float:left;
animation-name:changebackground;
animation-iteration-count:infinite;
animation-duration:2s;
animation-delay:5s;
}
If you need to slow down the animation then the property that needs to be modified is the animation's duration and not the animation delay. Set the animation-duration to a higher value. In the snippet, I have set it as 20s and so the change from each image to the next will take around 5s. If you need a time of 25s between each switch, then set the duration as 100s. animation-delay just adds a time delay before the start of the animation's first iteration but it doesn't really slow it down.
I don't really see a shake and so would need to see a demo of your code in-order to provide solutions. You may want to have a look at preloading all background images to stop it from causing problems.
#keyframes changebackground {
0% {
background-image: url("http://lorempixel.com/200/200/nature/4");
}
25% {
background-image: url("http://lorempixel.com/200/200/nature/1");
}
50% {
background-image: url("http://lorempixel.com/200/200/nature/2");
}
75% {
background-image: url("http://lorempixel.com/200/200/nature/3");
}
}
#banner {
margin-top: 2.9em;
background-image: url("http://lorempixel.com/200/200/nature/4");
background-position: center;
background-repeat: no-repeat;
padding: 22em 0em 8em 0em;
background-size: cover;
width: 100%;
float: left;
animation-name: changebackground;
animation-iteration-count: infinite;
animation-duration: 20s; /* set this */
animation-delay: 5s;
}
<section id="banner">
<div class="inner">
<h2>Enhancing Your <br />Ways</h2>
<p>A free platform for schedualing</p>
</div>
</section>

Keyframes animation working only on chrome/chromium

I have done a simple three image transition animation code. The code can be found here:
http://jsfiddle.net/harshithjv/AF3Jj/
This code works only on chrome and chromium browsers. It does not work on Apple's Safari browser also. Also it does not work on any other browsers(I tested on Firefox and IE9, not tried Opera).
I guess that I am missing something on animation shorthand property. Please help me out.
Edit:
I am updating with the code for some clarity, which I should have done in first place.
HTML Code:
<div class="animated_star"></div>
CSS3 Code:
#-moz-keyframes shining_star {
from {
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
to {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
#-webkit-keyframes shining_star {
from {
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
100% {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
#keyframes shining_star {
from{
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
to {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
.animated_star{
height: 16px;
width: 16px;
float: left;
-webkit-animation: shining_star 1s infinite; /* works only for Chrome/Chromium */
-moz-animation: shining_star 1s infinite;
animation: shining_star 1s infinite;
}
Background image isn't a property that can be animated - you can't tween the property.
Instead, try laying out all the images on top of each other using position:absolute, then animate the opacity of all of them to 0 except the one you want repeatedly.
also
It works in Chrome 19!
So at some point in the future, keyframes could really be... frames!
You are living in the future ;)
After some research on this, I figured that background-image CSS property is not supported inside keyframes in most browsers. It must be because of loading too many images dynamically can lead to performance issues if larger images are loaded.
Thanks to #Morpheus for another stackoverflow link(http://stackoverflow.com/questions/7318462/changing-background-image-with-css3-animations), through which I decided to resolve the issue through image sprites and reposition(using CSS property - background-position) within that sprite to select the image as I want it. The problem with background-position CSS property is that when it applied for CSS animation through keyframes, the reposition shows the movement within image sprite. But I wanted to show 3 stars transition quickly without movement in three frames. To make that possible, I had to use 6 keyframes where first star's position will be set in 0% and 33%, second star's position will be set in 34% and 66% and the third star will be set in 67% and 100%.
I have created a jsFiddle which does not have image sprites of same stars. I could not locate sprite for same stars online and so I used alternate stars. Its not a perfect example since it has sloppy animation, but I have created a smaller sprite image (48px x 16px) on my system, and animation looks good enough.
HTML Code:
<div class="animated_star"></div>
CSS Code:
#-moz-keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
#-webkit-keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
#keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
.animated_star{
height: 130px;
width: 135px;
float: left;
background: transparent url('http://azmind.com/wp-content/uploads/2011/11/social-star-icons.png') no-repeat fixed;
background-position: 0px -390px;
-webkit-animation: shining_star .5s infinite linear;
-moz-animation: shining_star .5s infinite linear;
animation: shining_star .5s infinite linear;
}
The jsFiddle link: http://jsfiddle.net/harshithjv/7QvSP/2/