jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

[ 241 ] Chapter 9 Setting the auto expand parameter causes the image to increase in size, which is a slight inconvenience to say the least. However if we don't set this, the element will be clipped, as shown in the following screenshot: Clipping pretty much destroys our simple box (and the content is still scaled), as we can see in the previous screenshot. CSS3 3D transforms All of the transform functions we have looked at so far are two-dimensional, operating on just the x and y axes. Transforms that operate in three dimensions, along x, y, and z axes have also been proposed. 3D equivalents of all of the transform functions exist and usually just take an extra parameter which corresponds to the vector of each dimension, and the angle. For example, a 3D rotation could be added using this code: transform: rotate3d(0, 1, 0, 30deg); As with 2D transforms, there is an all-encompassing matrix function that allows us to implement any of the other transforms and allows us to combine some of them together on a single element.

CSS3 Animations If, like me, you thought the 2D transform matrix, with its six parameters, was complex and perhaps a little hard to understand, wait till you start using the 3D matrix, which has 16 parameters in total! At present 3D transforms are only supported in Webkit-based browsers, so we won't be looking at these in any further detail. But hopefully they'll be appearing in more browsers sooner rather than later. Animated rotation with jQuery and CSS3 In this example, we'll set up an animation that rotates an image using the rotate() transform function. Because this is supported by the majority of common browsers it's actually really easy to implement, and can be a great effect that enhances the appearance and behavior of the page it is used on. Time for action – animating an element's rotation We'll just be rotating a simple image in this example, so this is the only visible element we need in the of the page. 1. Add the following to a fresh copy of the template file: 2. At this point we don't even need any styles as everything we need to set can be done in the JavaScript, which we'll add next. 3. In the function at the bottom of the HTML page, add the following code: var img = $("#colorWheel"), offset = img.offset(), origWidth = img.width(), origHeight = img.height(), rotateStrings = [ "rotate(", 0, "deg)" ], getVendor = function() { var prefix = null, vendorStrings = { pure: "transform", [ 242 ]

CSS3 <strong>Animation</strong>s<br />

If, like me, you thought the 2D transform matrix, with its six parameters, was complex and<br />

perhaps a little hard to understand, wait till you start using the 3D matrix, which has 16<br />

parameters in total!<br />

At present 3D transforms are only supported in Webkit-based browsers, so we won't be<br />

looking at these in any further detail. But hopefully they'll be appearing in more browsers<br />

sooner rather than later.<br />

Animated rotation with <strong>jQuery</strong> and CSS3<br />

In this example, we'll set up an animation that rotates an image using the rotate()<br />

transform function. Because this is supported by the majority <strong>of</strong> common browsers it's<br />

actually really easy to implement, and can be a great effect that enhances the appearance<br />

and behavior <strong>of</strong> the page it is used on.<br />

Time for action – animating an element's rotation<br />

We'll just be rotating a simple image in this example, so this is the only visible element we<br />

need in the <strong>of</strong> the page.<br />

1. Add the following to a fresh copy <strong>of</strong> the template file:<br />

<br />

2. At this point we don't even need any styles as everything we need to set can be<br />

done in the JavaScript, which we'll add next.<br />

3. In the function at the bottom <strong>of</strong> the HTML page, add the following code:<br />

var img = $("#colorWheel"),<br />

<strong>of</strong>fset = img.<strong>of</strong>fset(),<br />

origWidth = img.width(),<br />

origHeight = img.height(),<br />

rotateStrings = [<br />

"rotate(",<br />

0,<br />

"deg)"<br />

],<br />

getVendor = function() {<br />

var prefix = null,<br />

vendorStrings = {<br />

pure: "transform",<br />

[ 242 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!