jQuery Patch: Animate CSS Rotation and Scale
- published:
- 2009.08.07
- topics:
- css
- javascript
I have created a monkey patch for jQuery 1.3.1+ which enables you to independently set and/or animate both the scale and rotation of any HTML content with jQuery. This code uses the scale() and rotate() CSS transformations that are only supported by Webkit, Safari, Chrome, and Firefox 3.5 at this time.
As an example I have some spinning HTML content below. You can click the content to increase its scale while it continues to spin. Try it out:
After applying my patch, the following jQuery code is used to create this example:
setInterval(
function () {
$('#at_ex1').animate({rotate: '+=10deg'}, 0);
},
200
);
$('#at_ex1').click(
function () {
$(this).animate({scale: '+=0.33'}, {queue: false, duration: 1000});
}
);
Features of this Patch:
- Get and set rotate and scale CSS transforms independently with the added custom jQuery methods
rotate()andscale(). These work like setting any other CSS property, i.e. you are rotating to an angle not rotating by an angle. Example syntax:$('#example').rotate('30deg'); // rotates to 30deg $('#example').scale(1.5); // scales to 150% $('#example').rotate('45deg'); // rotates to 45deg $('#example').rotate(); // will return '45deg' $('#example').scale(); // will return '1.5' - Animate rotate and scale CSS transforms independently using jQuery's
animate()method. This works like any other jQuery CSS animation, i.e. you can rotate to a specific angle, or you can increase or decrease the angle of rotation by any number of degrees. Here are examples of both syntaxes:// Rotate to 30deg and scale to 125% $('#example').animate({rotate: '30deg', scale: '1.25'}, 1000); // Rotate by 30deg from current position and scale down by 10% $('#example').animate({rotate: '+=30deg', scale: '-=0.1'}, 1000);
Download and Usage
Download a zip file of the patch now or visit the project on GitHub.
This patch requires another one of my jQuery patches that adds CSS transform support to jQuery. The file jquery-css-transform.js should be included in your download.
To use this patch, load jquery-animate-css-rotate-scale.js after loading jQuery and
jquery-css-transform.js. For example:
<script src="jquery.js" type="text/javascript"></script> <script src="jquery-css-transform.js" type="text/javascript"></script> <script src="jquery-animate-css-rotate-scale.js" type="text/javascript"></script>