Cross-browser rotation with jQuery.animate() proves challenging due to the non-animation capabilities of CSS-Transforms. The code below demonstrates the issue:
$(document).ready(function () { DoRotate(30); AnimateRotate(30); }); function DoRotate(d) { $("#MyDiv1").css({ '-moz-transform':'rotate('+d+'deg)', '-webkit-transform':'rotate('+d+'deg)', '-o-transform':'rotate('+d+'deg)', '-ms-transform':'rotate('+d+'deg)', 'transform': 'rotate('+d+'deg)' }); } function AnimateRotate(d) { $("#MyDiv2").animate({ '-moz-transform':'rotate('+d+'deg)', '-webkit-transform':'rotate('+d+'deg)', '-o-transform':'rotate('+d+'deg)', '-ms-transform':'rotate('+d+'deg)', 'transform':'rotate('+d+'deg)' }, 1000); }
Rotation works when using .css(), but not with .animate(). Why? And how can we overcome this obstacle?
While CSS-Transforms lack direct animation support in jQuery, a workaround is possible using a step-callback like this:
function AnimateRotate(angle) { // Cache the object for performance var $elem = $('#MyDiv2'); // Use a pseudo object for the animation (starts from `0` to `angle`) $({deg: 0}).animate({deg: angle}, { duration: 2000, step: function(now) { // Use the `now` parameter (current animation position) in the step-callback $elem.css({ transform: 'rotate(' + now + 'deg)' }); } }); }
This method allows for the rotation of elements using jQuery. Additionally, jQuery 1.7 eliminates the need for CSS3 transform prefixes.
To simplify the process, create a jQuery plugin like this:
$.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);
For better efficiency and flexibility, an optimized plugin can be created:
$.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); }); };
The plugin provides two ways to use it:
$(node).animateRotate(90); $(node).animateRotate(90, function () {}); $(node).animateRotate(90, 1337, 'linear', function () {});
$(node).animateRotate(90, { duration: 1337, easing: 'linear', complete: function () {}, step: function () {} });
This plugin enables cross-browser CSS rotation using jQuery's animation capabilities. It eliminates the need for manual rotation calculations and provides a convenient and optimized way to achieve rotation effects.
The above is the detailed content of Why doesn't jQuery.animate() work for CSS3 rotations, and how can we achieve cross-browser animated rotations using jQuery?. For more information, please follow other related articles on the PHP Chinese website!