So I want to rotate an icon in place using css.
I've tried this
.rotate-icon-gif {
-webkit-animation-name: spin;
-webkit-animation-duration: 4000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4000ms;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4000ms;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to { -ms-transform: rotate(360deg); }
}
#-moz-keyframes spin {
from { -moz-transform: rotate(0deg); }
to { -moz-transform: rotate(360deg); }
}
#-webkit-keyframes spin {
from { -webkit-transform: rotate(0deg);
-webkit-transform-origin: 30px 22px;
}
to { -webkit-transform: rotate(360deg);
-webkit-transform-origin: 30px 22px;
}
}
#keyframes spin {
from {
transform:rotate(0deg);
transform-origin: 55% 50%;
}
to {
transform:rotate(360deg);
transform-origin: 55% 50%;
}
}
But it won't rotate in place - it rotates about some other center.
I've looked at the dimensions of the div that has this -
It's 60 x 30, with 15 padding on top. Hence, I've tried to make the transform-origin on 30px and 22px offsets.
It's still not working - how should I go about fixing this?
There are two methods of achieving this. Firstly, you can edit the image so the center lies directly on the axis of rotation.
First Method
Using cropped image. (No CSS added, deleted obsolete transition-origin)
See snippet:
.rotate-icon-gif {
-webkit-animation-name: spin;
-webkit-animation-duration: 4000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4000ms;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4000ms;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to { -ms-transform: rotate(360deg); }
}
#-moz-keyframes spin {
from { -moz-transform: rotate(0deg); }
to { -moz-transform: rotate(360deg); }
}
#-webkit-keyframes spin {
from { -webkit-transform: rotate(0deg);
}
to { -webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform:rotate(0deg);
}
to {
transform:rotate(360deg);
}
}
<img src="http://i.imgur.com/tPLLoew.png" class="rotate-icon-gif">
Second Method
You can use transform-origin: x y; Here, you need to find the x and y coordinates of the axis using Paint or Photoshop like this: (You need to find the coordinates of intersection of black line)
See snippet :
.rotate-icon-gif {
-webkit-animation-name: spin;
-webkit-animation-duration: 4s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4s;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4s;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
transform-origin: 42px 35px;
}
to {
-ms-transform: rotate(360deg);
transform-origin: 42px 35px;
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
-moz-transform-origin: 42px 35px;
}
to {
-moz-transform: rotate(360deg);
-moz-transform-origin: 42px 35px;
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
-webkit-transform-origin: 42px 35px;
}
to {
-webkit-transform: rotate(360deg);
-webkit-transform-origin: 42px 35px;
}
}
#keyframes spin {
from {
transform: rotate(0deg);
transform-origin: 42px 35px;
}
to {
transform: rotate(360deg);
transform-origin: 42px 35px;
}
}
<img src="http://i.stack.imgur.com/GZ2CV.png" class="rotate-icon-gif">
NOTE - From my other answer : link
Related
I want to use a rotating/spinning letter as my loader on a website. I want it to rotate through the center.
I have used transform-origin: center center; as mentioned here. But no help.
Can some fix this? Thanks!
Also, Is this the right approach? Please mention if there is any better of achieving this?
(I don't want to go with the svg.)
CSS
.custom-spinner2 {
font-family: sans-serif;
color: black;
margin: 5rem;
width: 2rem;
height: auto;
line-height: 1rem;
transform-origin: center center;
-webkit-animation-name: spin;
-webkit-animation-duration: 1000ms;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: spin;
-moz-animation-duration: 1000ms;
-moz-animation-iteration-count: infinite;
-ms-animation-name: spin;
-ms-animation-duration: 1000ms;
-ms-animation-iteration-count: infinite;
animation-name: spin;
animation-duration: 1000ms;
animation-iteration-count: infinite;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
**HTML**
<p class="custom-spinner2" style="font-size:5rem !important;">W</p>
Do this. First, centralize the letter both vertically and horizontally to the center of the page. Then apply rotation animation to the letter. It should work. Don't add any sort of width to the text. Otherwise, the text will left align itself in that width and the animation will look weird.
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
p {
animation: Rotate infinite 1s;
font-size: 50px;
}
#keyframes Rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<p>M</p>
Change the display from block to inline-block and remove the width:
.custom-spinner2 {
font-family: sans-serif;
color: black;
margin: 5rem;
display: inline-block;
height: auto;
line-height: 1rem;
transform-origin: center center;
-webkit-animation-name: spin;
-webkit-animation-duration: 1000ms;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: spin;
-moz-animation-duration: 1000ms;
-moz-animation-iteration-count: infinite;
-ms-animation-name: spin;
-ms-animation-duration: 1000ms;
-ms-animation-iteration-count: infinite;
animation-name: spin;
animation-duration: 1000ms;
animation-iteration-count: infinite;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
**HTML**
<p class="custom-spinner2" style="font-size:5rem !important;">W</p>
width is set to 2 rem, which makes the letter to go outside the container, around whose center it rotates (the letter needs more space). Set width to 5rem and you are set:
.custom-spinner2 {
font-family: sans-serif;
color: black;
margin: 5rem;
width: 5rem;
height: auto;
line-height: 1rem;
transform-origin: center center;
-webkit-animation-name: spin;
-webkit-animation-duration: 1000ms;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: spin;
-moz-animation-duration: 1000ms;
-moz-animation-iteration-count: infinite;
-ms-animation-name: spin;
-ms-animation-duration: 1000ms;
-ms-animation-iteration-count: infinite;
animation-name: spin;
animation-duration: 1000ms;
animation-iteration-count: infinite;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
**HTML**
<p class="custom-spinner2" style="font-size:5rem !important;">W</p>
I am trying to animate a div to spin 360deg and move 400px to the right. How can I do this using CSS3? Do I need to use CSS3 keyframes?
<div id="spin"></div>
CSS:
#spin {
width:200px;
height:200px;
background-color:blue;
}
Yes, you need keyframes:
#spin {
width: 200px;
height: 200px;
background-color: blue;
-webkit-animation: myanimation 5s;
animation: myanimation 5s;
}
#-webkit-keyframes myanimation {
100% { margin-left: 400px; -webkit-transform: rotate(360deg); }
}
#keyframes myanimation {
100% { margin-left: 400px; transform: rotate(360deg); }
}
<div id="spin"></div>
add all the prefixes so it works on all modern browsers
-webkit-
-moz-
-ms-
-o-
Try this and adjust to your needs:
#spin {
position: absolute;
width: 200px;
height: 200px;
background: #00f;
-webkit-animation-name: spin;
-webkit-animation-duration: 4000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4000ms;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4000ms;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform: rotate(0deg);
left: 0px;
}
to {
transform: rotate(360deg);
left: 400px;
}
}
<div id="spin"></div>
<img class="image" src="" alt="" width="120" height="120">
Cannot get this animated image to work, it is supposed to do a 360 degrees rotation.
I guess something's wrong with the CSS below, as it just stays still.
.image {
float: left;
margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin-top: -60px;
margin-left: -60px;
-webkit-animation-name: spin;
-webkit-animation-duration: 4000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4000ms;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4000ms;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
} to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
} to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
} to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform: rotate(0deg);
} to {
transform: rotate(360deg);
}
}
}
Here is a demo. The correct animation CSS:
.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin:-60px 0 0 -60px;
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
#-moz-keyframes spin {
100% { -moz-transform: rotate(360deg); }
}
#-webkit-keyframes spin {
100% { -webkit-transform: rotate(360deg); }
}
#keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform:rotate(360deg);
}
}
<img class="image" src="http://i.stack.imgur.com/pC1Tv.jpg" alt="" width="120" height="120">
Some notes on your code:
You've nested the keyframes inside the .image rule, and that's incorrect
float:left won't work on absolutely positioned elements
Have a look at caniuse: IE10 doesn't need the -ms- prefix
To achieve the 360 degree rotation, here is the Working Solution.
The HTML:
<img class="image" src="your-image.png">
The CSS:
.image {
overflow: hidden;
transition-duration: 0.8s;
transition-property: transform;
}
.image:hover {
transform: rotate(360deg);
-webkit-transform: rotate(360deg);
}
You have to hover on the image and you will get the 360 degree rotation effect.
PS: Add a -webkit- extension for it to work on chrome and other webkit browers. You can check the updated fiddle for webkit HERE
I have a rotating image using the same thing as you:
.knoop1 img{
position:absolute;
width:114px;
height:114px;
top:400px;
margin:0 auto;
margin-left:-195px;
z-index:0;
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
-o-transition-duration: 0.8s;
transition-duration: 0.8s;
-webkit-transition-property: -webkit-transform;
-moz-transition-property: -moz-transform;
-o-transition-property: -o-transform;
transition-property: transform;
overflow:hidden;
}
.knoop1:hover img{
-webkit-transform:rotate(360deg);
-moz-transform:rotate(360deg);
-o-transform:rotate(360deg);
}
try this easy
.btn-circle span {
top: 0;
position: absolute;
font-size: 18px;
text-align: center;
text-decoration: none;
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
.btn-circle span :hover {
color :silver;
}
/* rotate 360 key for refresh btn */
#-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
#-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
#keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<button type="button" class="btn btn-success btn-circle" ><span class="glyphicon">↻</span></button>
if you want to flip image you can use it.
.image{
width: 100%;
-webkit-animation:spin 3s linear infinite;
-moz-animation:spin 3s linear infinite;
animation:spin 3s linear infinite;
}
#-moz-keyframes spin { 50% { -moz-transform: rotateY(90deg); } }
#-webkit-keyframes spin { 50% { -webkit-transform: rotateY(90deg); } }
#keyframes spin { 50% { -webkit-transform: rotateY(90deg); transform:rotateY(90deg); } }
The another method to rotate an object in the background using css3, check out the below css3 code here:
.floating-ball-model-3 > span {
animation-name: floating-ball-model-3;
animation-duration: 7s;
animation-iteration-count: infinite;
animation-timing-function: linear;
-webkit-animation-name: floating-ball-model-3;
-webkit-animation-duration: 7s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: floating-ball-model-3;
-moz-animation-duration: 7s;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: floating-ball-model-3;
-ms-animation-duration: 7s;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
-o-animation-name: floating-ball-model-3;
-o-animation-duration: 7s;
-o-animation-iteration-count: infinite;
-o-animation-timing-function: linear;
}
#keyframes floating-ball-model-3 {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Here this should help you
The below jsfiddle link will help you understand how to rotate a image.I used the same one to rotate the dial of a clock.
http://jsfiddle.net/xw89p/
var rotation = function (){
$("#image").rotate({
angle:0,
animateTo:360,
callback: rotation,
easing: function (x,t,b,c,d){
return c*(t/d)+b;
}
});
}
rotation();
Where:
• t: current time,
• b: begInnIng value,
• c: change In value,
• d: duration,
• x: unused
No easing (linear easing):
function(x, t, b, c, d) { return b+(t/d)*c ; }
I got some css for rotating gears and so far they work great in Firefox but do not work in IE. Can anyone provide a fix to this problem.
Here is my html to reference the classes:
<div class="gear" id="gear1"></div>
<div class="gear" id="gear2"></div>
<div class="gear" id="gear3"></div>
<div class="gear" id="gear4"></div>
<div class="gear" id="gear5"></div>
<div class="gear" id="gear6"></div>
<div class="gear" id="gear7"></div>
Here is my css:
/* CSS3 keyframes */
#-webkit-keyframes ckw {
0% {
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg);
}
}
#-moz-keyframes ckw {
0% {
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg);
}
}
#-webkit-keyframes cckw {
0% {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg);
}
100% {
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#-moz-keyframes cckw {
0% {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg);
}
100% {
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#keyframes cckw {
0% {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-ms-transform: rotate(360deg);
-o-transform: rotate(360deg);
transform: rotate(360deg);
}
100% {
-moz-transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
}
}
/* gears */
.gear {
float: none;
position: absolute;
text-align: center;
-moz-animation-timing-function: linear;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: normal;
-o-animation-direction: normal;
-moz-animation-delay: 0;
-o-animation-delay: 0;
-moz-animation-play-state: running;
-moz-animation-fill-mode: forwards;
-webkit-animation-timing-function: linear;
-o-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
-o-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-direction: normal;
animation-direction: normal;
-webkit-animation-delay: 0;
animation-delay: 0;
-webkit-animation-play-state: running;
-o-animation-play-state: running;
animation-play-state: running;
-webkit-animation-fill-mode: forwards;
-o-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
#gear1 {
background: url('g1.png') no-repeat 0 0;
height: 85px;
left: 31px;
top: 45px;
width: 85px;
-moz-animation-name: ckw;
-moz-animation-duration: 10s;
-webkit-animation-name: ckw;
-o-animation-name: ckw;
animation-name: ckw;
-webkit-animation-duration: 10s;
-o-animation-duration: 10s;
animation-duration: 10s;
}
#gear2 {
background: url('g2.png') no-repeat 0 0;
height: 125px;
left: 105px;
top: 10px;
width: 125px;
-moz-animation-name: cckw;
-moz-animation-duration: 16.84s;
-webkit-animation-name: cckw;
-o-animation-name: cckw;
animation-name: cckw;
-webkit-animation-duration: 16.84s;
-o-animation-duration: 16.84s;
animation-duration: 16.84s;
}
#gear3 {
background: url('g3.png') no-repeat 0 0;
height: 103px;
left: 149px;
top: 118px;
width: 103px;
-moz-animation-name: ckw;
-moz-animation-duration: 13.5s;
-webkit-animation-name: ckw;
-o-animation-name: ckw;
animation-name: ckw;
-webkit-animation-duration: 13.5s;
-o-animation-duration: 13.5s;
animation-duration: 13.5s;
}
#gear4 {
background: url('g4.png') no-repeat 0 0;
height: 144px;
left: 46px;
top: 173px;
width: 144px;
-moz-animation-name: cckw;
-moz-animation-duration: 20.2s;
-webkit-animation-name: cckw;
-o-animation-name: cckw;
animation-name: cckw;
-webkit-animation-duration: 20.2s;
-o-animation-duration: 20.2s;
animation-duration: 20.2s;
}
#gear5 {
background: url('g1.png') no-repeat 0 0;
height: 85px;
left: 127px;
top: 292px;
width: 85px;
-moz-animation-name: ckw;
-moz-animation-duration: 10s;
-webkit-animation-name: ckw;
-o-animation-name: ckw;
animation-name: ckw;
-webkit-animation-duration: 10s;
-o-animation-duration: 10s;
animation-duration: 10s;
}
#gear6 {
background: url('g2.png') no-repeat 0 0;
height: 125px;
left: 200px;
top: 283px;
width: 125px;
-moz-animation-name: cckw;
-moz-animation-duration: 16.84s;
-webkit-animation-name: cckw;
-o-animation-name: cckw;
animation-name: cckw;
-webkit-animation-duration: 16.84s;
-o-animation-duration: 16.84s;
animation-duration: 16.84s;
}
#gear7 {
background: url('g3.png') no-repeat 0 0;
height: 103px;
left: 277px;
top: 217px;
width: 103px;
-moz-animation-name: ckw;
-moz-animation-duration: 13.5s;
-webkit-animation-name: ckw;
-o-animation-name: ckw;
animation-name: ckw;
-webkit-animation-duration: 13.5s;
-o-animation-duration: 13.5s;
animation-duration: 13.5s;
}
If you're using anything older than IE 10, keyframe animations are not going to work. See Caniuse for compatibility tables, and consider a JavaScript/jQuery alternative or polyfill if you need older browser support.
EDIT: It looks like some of your CSS (i.e., you .gear class) is missing the -ms prefix, which will give you somewhat broader compatibility.
I looking for changing my background-color after a css rotateY when my div is hover
#-webkit-keyframes spin {
0%{
-webkit-transform: rotateY(0deg); -webkit-transform-origin: 0% 0% 5;
}
100%{
-webkit-transform: rotateY(180deg); -webkit-transform-origin: 0% 0% 5;
}
}
.hex:hover{
background:red;
-webkit-animation-name: spin;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-duration: 1s;
}
You should define that in you keyframe as your element is animating on hover so you can change the background color in the animation keyframe itself.
Try this:
#-webkit-keyframes spin {
0%{
-webkit-transform: rotateY(0deg); -webkit-transform-origin: 0% 0% 5;
}
100%{
-webkit-transform: rotateY(180deg); -webkit-transform-origin: 0% 0% 5;
background:red;
}
}
.hex:hover{
-webkit-animation-name: spin;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-duration: 1s;
}
.hexHovered {
background:red;
}
To retain the backgroud color at hover you can use this javascript code.
$(".hex").hover(
function () { $(this).addClass(".hexHovered")
});
You can make the animation "persist" in time using animation-fill-mode.
Asuming that you want the color to persist, but not the rotation, it is alittle bit more complicated; you need two animations so that you can make only one persistent.
#-webkit-keyframes spin {
0% { -webkit-transform: rotateY(0deg); }
100%{ -webkit-transform: rotateY(180deg); }
}
#-webkit-keyframes red {
0% { background: white; }
100% { background: red; }
}
.hex {
-webkit-animation-fill-mode: none, forwards;
-webkit-animation-name: spin, red;
-webkit-animation-iteration-count: 0;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-duration: 1s, 0.1s;
-webkit-animation-delay: 0s 1s;
-webkit-transform-origin: 0% 0% 5;
}
.hex:hover{
-webkit-animation-iteration-count: 1;
}
fiddle