markzzz
markzzz

Reputation: 47945

Animate rotation on "CSS3"

If I have this box in CSS3 (also if, for what I understand, this is not CSS3, just browsers specific) :

HTML

<div id="container">
    <div id="box">&nbsp;</div>
</div>    ​

CSS

#container
{
    padding:100px;
}

#box
{
    width:200px;
    height:200px;
    background-color:red;
}

#box.selected
{
    transform: rotate(30deg);
    -ms-transform: rotate(30deg); /* IE 9 */
    -webkit-transform: rotate(30deg); /* Safari and Chrome */
    -o-transform: rotate(30deg); /* Opera */
    -moz-transform: rotate(30deg); /* Firefox */        
}
​

jQuery (just for manage the hover on the box)

$('#box').hover(
    function () {
        $(this).addClass('selected');
    },
    function () {
        $(this).removeClass('selected');
    }
);

how can I set an animation to that css rotation? I mean, not in 1 step, but fluid. So the moviment should be "animate". Hope you understand what I mean :)

Upvotes: 3

Views: 636

Answers (4)

Hubro
Hubro

Reputation: 59323

Here's what you're trying to do:

http://jsfiddle.net/fud4n/18/

It's important to remember to but the origin and transition properties in the class/id of the element you're trying to animate, not the class representing the animated state.

Cheers

Upvotes: 1

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123377

Use a CSS3 animation like so: http://jsfiddle.net/fud4n/15/ (example given for firefox only). This will perform a continuos rotation, just change the duration as you prefer.

#box.selected {    
    -moz-animation-name: rotation;  
    -moz-animation-duration: 2s;  
}

@-moz-keyframes rotation {
    from {  -moz-transform: rotate(0deg);  }      
    to   {  -moz-transform: rotate(30deg);  }
}  

Upvotes: 0

Rudi Visser
Rudi Visser

Reputation: 21969

Assuming you just want to apply an animation to the transformation, you can use CSS3 transitions, specifically transition-property (defines which properties will have a transition) and transition-duration (to specify the duration of the transition from start to completion). There is also transition-timing-function, which allows you to use any of the following modes of transition: linear|ease|ease-in|ease-out|ease-in-out|cubic-bezier(n,n,n,n)

#box
{
    width:200px;
    height:200px;
    background-color:red;
    transition-property: all;
    transition-duration: 0.3s;
    /* Explicit above, can also use shorthand */
    -o-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -webkit-transition: all 0.3s;
    -ms-transition: all 0.3s;
    /* Also shorthand with the easing-function */
    -ms-transition: all 0.3s linear;
}

See my revision to your jsfiddle -> http://jsfiddle.net/fud4n/9/

Upvotes: 5

Christian
Christian

Reputation: 19740

You can use CSS transitions like so:

-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;

Upvotes: 2

Related Questions