


Why doesn't jQuery.animate() work for CSS3 rotations, and how can we achieve cross-browser animated rotations using jQuery?
Cross-Browser Rotation with jQuery.animate()
Problem
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?
Solution
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.
jQuery Plugin
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);
Optimized Plugin
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); }); };
Usage
The plugin provides two ways to use it:
- Single line syntax:
$(node).animateRotate(90); $(node).animateRotate(90, function () {}); $(node).animateRotate(90, 1337, 'linear', function () {});
- Object syntax (preferred for more than three arguments):
$(node).animateRotate(90, { duration: 1337, easing: 'linear', complete: function () {}, step: function () {} });
Conclusion
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
