Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate in CSS3 around origin?

Tags:

css

I have the following letters ABC as shown below:

 <body>
 <div id="container">
   <div id="shape" class="spin">
     <div id="A" class="plane">A</div>
     <div id="B" class="plane">B</div>    
     <div id="C" class="plane">C</div>
   </div>
 </div>

What I want is each letter to spin around its x-axis?

I tried (for letter C):

#C {
 -webkit-animation: spinAboutItsCentre 8s linear; 
}

@-webkit-keyframes spinAboutItsCentre {
  from { 
        -webkit-transform: rotateX(0); 
  }
  to   { 

   -webkit-transform: rotateX(360deg); 
  }
}

but the letter C moves over to where the letter A is an spins it its axis.

Any ideas?

JD

like image 690
JD. Avatar asked Aug 10 '11 16:08

JD.


2 Answers

It should look something like this, you need to specify your transform-origin properties; x-%, y-%, and z-px.

Notice spinning about the Y-axis creates a bit of offset because the engine's interpretation of the character's position originates at the "beginning" (side) of the object, not the center of the object.

The 0% and 100% designations represent your "from" and "to" clauses, this format allows you to add as many of these lines as you wish to increment the movement over your specified timeframe (i.e. 25% rotate 90deg, 50% rotate 180deg, 75% rotate 270deg, 100% rotate 360deg).

@-webkit-keyframes spinX
{  
0%   {-webkit-transform: rotateX(0deg); -webkit-transform-origin: 0% 50% 0;}  
100% {-webkit-transform: rotateX(360deg); -webkit-transform-origin: 0% 50% 0;}  
}

@-webkit-keyframes spinY
{  
0%   {-webkit-transform: rotateY(0deg); -webkit-transform-origin: 0% 0% 5;}  
100% {-webkit-transform: rotateY(360deg); -webkit-transform-origin: 0% 0% 5;}  
}  

Try these styles, they should work alright.

#Ca
{  
position: absolute;  
left: 20%;  
font-size:72px;  
-webkit-animation: spinX 8s infinite;  
}

#Cb
{  
position: absolute;  
left: 20%;  
font-size:72px;  
-webkit-animation: spinY 8s infinite;  
}

<div id="Ca">C</div>  
<div id="Cb">C</div>
like image 118
Chris Avatar answered Nov 15 '22 12:11

Chris


Transforms have a "transform-origin" associated with them. When no transform origin is specified, it is automatically set at (50%, 50%) of the element. When your exact code is entered as a jsfiddle, it works as intended.

My guess is that in your complete code you have specified a transform origin incorrectly or have other weirdness in the base CSS for your class.

Update: So, yes, you had weirdness in the base CSS. It would be helpful to see your complete CSS and HTML for debugging.

like image 45
Michael Mullany Avatar answered Nov 15 '22 12:11

Michael Mullany