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 />

Calculating the sine and cosine functions <strong>of</strong> the angle <strong>of</strong> rotation can easily be done using<br />

a scientific calculator, or <strong>of</strong> course, JavaScript itself programmatically.<br />

Working with transforms<br />

Using the short-cut transform functions such as rotate(), or skew() is easier and more<br />

convenient than using the matrix. However, this ease <strong>of</strong> use comes at a price—we're limited<br />

to only using one <strong>of</strong> them at a time on a single element. If we were to try and use more than<br />

one <strong>of</strong> them in a CSS statement, only the last one defined would be applied.<br />

If we need to apply several different transforms to an element, we can use the matrix<br />

function, depending on which transformations we need to apply. For example, we can<br />

skew an element, while also translating and scaling it using something like the following:<br />

transform: matrix(2, -1, 0, 2, 300px, 0);<br />

In this example, the element would be skewed along the x axis, doubled in size and moved<br />

300 px to the right. We couldn't rotate the targeted element in the previous code-snippet at<br />

the same time as doing these things.<br />

Even if we supply two matrix functions, one for the skew, scale and translate, and a second<br />

for the rotation, only the rotation would be applied. We can however rotate and translate,<br />

or rotate and scale an element simultaneously using a single matrix function.<br />

<strong>jQuery</strong> and transforms<br />

We can use <strong>jQuery</strong>'s css() method in setter mode to set CSS3 transforms on selected<br />

elements, and we can use it in getter mode to retrieve any transform functions set on an<br />

element. We just need to ensure that we use the correct vendor prefix, such as –moztransform<br />

for Firefox, or -webkit-transform for Webkit-based browsers. Opera also<br />

has its own vendor prefix, as do newer versions <strong>of</strong> IE.<br />

One thing to be aware <strong>of</strong> is that while we can set a specific transform function, such as<br />

rotate(), on a selected element, we can only get the value <strong>of</strong> the style property in its<br />

matrix format. Look at the following code:<br />

$("#get").css("-moz-transform", "rotate(30deg)");<br />

$("#get").text($("#get").css("-moz-transform"));<br />

This would result in the following:<br />

[ 238 ]

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

Saved successfully!

Ooh no, something went wrong!