How can I animate my image infinitely using CSS3

2020-04-09 04:32发布

问题:

I have an image that has these styles:

<img src="images/head-tails.gif" class="graphs" />
.graphs {
    -webkit-transition: all 2s ease;
    -moz-transition: all 2s ease;
    -o-transition: all 2s ease;
    transition: all 2s ease;
    /* -webkit-animation:animated 5s infinite; */
    /* -moz-animation:animated 5s infinite; */
    /* -o-animation:animated 5s infinite; */
    /* animation:animated 5s infinite; */
}
.graphs:hover {
    -webkit-transform: rotateY(360deg);
    -moz-transform: rotateY(360deg);
    -o-transform: rotateY(360deg);
    transform: rotateY(360deg);
}

So when I hover over the image it rotates 360 degrees, like a coin. What I would like to make though is when the page loads, the image will do this animation (the rotation) infinitely, without need to hover over my mouse. How I can make this work?

回答1:

You can use keyframe animation for this. Write like this:

.graphs {
    -webkit-animation-iteration-count:infinite;
    -webkit-animation-timing-function:linear;
    -webkit-animation-name:orbit;
    -webkit-animation-duration:2s;
    -moz-animation-iteration-count:infinite;
    -moz-animation-timing-function:linear;
    -moz-animation-name:orbit;
    -moz-animation-duration:2s;
}
@-webkit-keyframes orbit { 
from { -webkit-transform:rotateY(0deg) } 
to { -webkit-transform:rotateY(360deg) } 
}
@-moz-keyframes orbit { 
from { -moz-transform:rotateY(0deg) } 
to { -moz-transform:rotateY(360deg) } 
}

Check this http://jsfiddle.net/ktPev/1/



回答2:

Use keyframe animations instead. Uncomment the commented code and change the animation-timing-function to linear. Like this animation: animated 5s linear infinite;. Then define the keyframe animation, for example like this (I've used -webkit- in my example):

@-webkit-keyframes animated {
        to { -webkit-transform: rotateY(360deg); }
}

and it should work!

Here's a DEMO