02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

}<br />

el.filters.item("DXImageTransform.Micros<strong>of</strong>t.Matrix")<br />

.M12 = -sin;<br />

el.filters.item("DXImageTransform.Micros<strong>of</strong>t.Matrix")<br />

.M21 = sin;<br />

el.filters.item("DXImageTransform.Micros<strong>of</strong>t.Matrix")<br />

.M22 = cos;<br />

} else {<br />

img.css(vendor, rotateStrings.join(""));<br />

}<br />

setInterval(function() { doRotate() }, 100);<br />

4. Save the page as rotate.html. If we run the page in a browser now, we should see<br />

the color wheel slowly spinning around its center.<br />

What just happened?<br />

The first thing we do is cache a selector for the image as we'll be referring to it several times<br />

throughout the code. Note that this is the only <strong>jQuery</strong> object we create in the whole script,<br />

which as we've discussed earlier in the book, is great for improving performance.<br />

We also set some other variables at this point including the <strong>of</strong>fset <strong>of</strong> the image<br />

(its absolute position on the page), its original width and height, and an array<br />

containing different parts <strong>of</strong> the CSS property that we'll set, in string and integer formats.<br />

We also set an inline function (getVendor()) as the value <strong>of</strong> a variable which we can use<br />

to determine which vendor prefix to use. This function first also sets some variables which<br />

will be used to store the determined vendor prefix, and an object literal containing all <strong>of</strong><br />

the different prefixes we want to test for. We also include the native transform property.<br />

Although this isn't yet supported by any browser, one day it may be, so this helps futurepro<strong>of</strong><br />

our code.<br />

The function iterates over each property in the object literal using a for in loop. Within<br />

the loop, we try to read the value <strong>of</strong> the transform property using each vendor prefix. An<br />

interesting fact is that each browser will report none as the value <strong>of</strong> the prefix it supports,<br />

and a falsey value such as false, null, or undefined for the prefixes it doesn't support.<br />

We can use this to reliably determine which browser is in use and therefore which vendor<br />

prefix we need to use. The correct vendor prefix for whichever browser is in use is then<br />

saved to the vendor variable ready to be returned.<br />

[ 244 ]

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

Saved successfully!

Ooh no, something went wrong!