Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CSS-Transforms are not possible to animate with jQuery, yet. You can do something like this:</p> <pre><code>function AnimateRotate(angle) { // caching the object for performance reasons var $elem = $('#MyDiv2'); // we use a pseudo object for the animation // (starts from `0` to `angle`), you can name it as you want $({deg: 0}).animate({deg: angle}, { duration: 2000, step: function(now) { // in the step-callback (that is fired each step of the animation), // you can use the `now` paramter which contains the current // animation-position (`0` up to `angle`) $elem.css({ transform: 'rotate(' + now + 'deg)' }); } }); } </code></pre> <p>You can read more about the step-callback here: <a href="http://api.jquery.com/animate/#step" rel="noreferrer">http://api.jquery.com/animate/#step</a></p> <p><a href="http://jsfiddle.net/UB2XR/23/" rel="noreferrer">http://jsfiddle.net/UB2XR/23/</a></p> <p>And, btw: you don't need to prefix css3 transforms with jQuery 1.7+</p> <h3>Update</h3> <p>You can wrap this in a jQuery-plugin to make your life a bit easier:</p> <pre><code>$.fn.animateRotate = function(angle, duration, easing, complete) { return this.each(function() { var $elem = $(this); $({deg: 0}).animate({deg: angle}, { duration: duration, easing: easing, step: function(now) { $elem.css({ transform: 'rotate(' + now + 'deg)' }); }, complete: complete || $.noop }); }); }; $('#MyDiv2').animateRotate(90); </code></pre> <p><a href="http://jsbin.com/ofagog/2/edit" rel="noreferrer">http://jsbin.com/ofagog/2/edit</a></p> <h3>Update2</h3> <p>I optimized it a bit to make the order of <code>easing</code>, <code>duration</code> and <code>complete</code> insignificant.</p> <pre><code>$.fn.animateRotate = function(angle, duration, easing, complete) { var args = $.speed(duration, easing, complete); var step = args.step; return this.each(function(i, e) { args.complete = $.proxy(args.complete, e); args.step = function(now) { $.style(e, 'transform', 'rotate(' + now + 'deg)'); if (step) return step.apply(e, arguments); }; $({deg: 0}).animate({deg: angle}, args); }); }; </code></pre> <h3>Update 2.1</h3> <p>Thanks to <a href="https://stackoverflow.com/questions/15191058/css-rotation-cross-browser-with-jquery-animate/15191130?noredirect=1#comment37890839_15191130">matteo</a> who noted an issue with the <code>this</code>-context in the complete-<code>callback</code>. If fixed it by <em>binding</em> the callback with <code>jQuery.proxy</code> on each node.</p> <p>I've added the edition to the code before from <strong>Update 2</strong>.</p> <h3>Update 2.2</h3> <p>This is a possible modification if you want to do something like toggle the rotation back and forth. I simply added a start parameter to the function and replaced this line:</p> <pre><code>$({deg: start}).animate({deg: angle}, args); </code></pre> <p>If anyone knows how to make this more generic for all use cases, whether or not they want to set a start degree, please make the appropriate edit.</p> <hr> <h2>The Usage<sup>...is quite simple!</sup></h2> <p>Mainly you've two ways to reach the desired result. But at the first, let's take a look on the arguments:</p> <p><code>jQuery.fn.animateRotate(angle, duration, easing, complete)</code></p> <p>Except of "angle" are all of them optional and fallback to the default <code>jQuery.fn.animate</code>-properties:</p> <pre><code>duration: 400 easing: "swing" complete: function () {} </code></pre> <h3>1st</h3> <p>This way is the short one, but looks a bit unclear the more arguments we pass in.</p> <pre><code>$(node).animateRotate(90); $(node).animateRotate(90, function () {}); $(node).animateRotate(90, 1337, 'linear', function () {}); </code></pre> <h3>2nd</h3> <p>I prefer to use objects if there are more than three arguments, so this syntax is my favorit:</p> <pre><code>$(node).animateRotate(90, { duration: 1337, easing: 'linear', complete: function () {}, step: function () {} }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload