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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

An unfortunate problem that occurs in IE when rotating elements is that the rotated element<br />

drifts around the page as it is being rotated. This is caused by the size <strong>of</strong> the elements<br />

bounding box increasing as the element rotates. The rotation does occur around the center<br />

<strong>of</strong> the element, but because IE thinks the element has grown, the center <strong>of</strong> the rotated<br />

element is shifted on each rotation.<br />

The drifX and driftY variables that we set allow us to determine how far the element<br />

has shifted so that we can correct it. The shift is worked out by comparing the original width<br />

and height <strong>of</strong> the element prior to it being rotated, with the new width and height<br />

following the rotation.<br />

We also store the raw img element from the <strong>jQuery</strong> object using <strong>jQuery</strong>'s get() method<br />

with an argument <strong>of</strong> 0 which returns the actual DOM node instead <strong>of</strong> a <strong>jQuery</strong> object. The<br />

filter must be applied to a proper DOM element.<br />

Once we've set our variables, we then correct the drift caused by the previous rotation<br />

using <strong>jQuery</strong>'s css() method, and then insert our computed trigonometry values into<br />

the Matrix filter.<br />

Finally, if the vendor variable equals anything other than filter we can simply set<br />

the relevant vendor prefix to the items in our rotateStrings array. We do this by<br />

calling JavaScript's join() method on the array. This is much more efficient than using<br />

concatenation to create the string needed for the CSS property, and as this function will be<br />

executed repeatedly we really need to make sure it is as efficient as possible.<br />

The last thing we do in our code is start the rotation animation <strong>of</strong>f by setting an interval that<br />

calls our doRotate() function every 100 milliseconds. We use an anonymous function as<br />

the first argument <strong>of</strong> the setInterval() function which avoids requiring that we attach<br />

the function to be executed be saved to the window object.<br />

Problems with IE<br />

Aside from the fact that IE makes us work twice as hard as any other browser to set the<br />

element's rotation, it also presents us with another problem: it totally destroys the alpha<br />

layer <strong>of</strong> the PNG we are rotating. Suddenly our nice anti-aliased circle-edge becomes jagged<br />

and unsightly (view this example in IE to see the issue).<br />

The animation is also slightly jerky in IE, and both this and the inability to use PNGs with<br />

alpha-layers in them could easily be a show-stopper for IE. If this was the case, we could<br />

easily disable the animation in IE by simply doing nothing when the filter property is<br />

returned by our getVendor() function. There are some things we could do however, to<br />

negate the problems in IE.<br />

[ 246 ]

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

Saved successfully!

Ooh no, something went wrong!