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.

Full Page <strong>Animation</strong>s<br />

Next we define the removeFrame() function which will be executed by setInterval.<br />

All this function does is check whether there is more than one element within the frames<br />

container and if so, remove the first one. Every 50 milliseconds, the top image will be<br />

removed, which is fast enough for the repeated showing <strong>of</strong> still images to be perceived<br />

as an animation. If there is only one image left, we clear the timeout as the animation<br />

has completed.<br />

Next we define the for loop, specifying the maximum number <strong>of</strong> frames, and decrementing<br />

on each iteration <strong>of</strong> the loop. We don't need to specify a comparison condition in this form<br />

<strong>of</strong> loop however, because the loop will naturally end when x = 0 (because 0 is a falsey<br />

value). Using decrementing for loops is a proven strategy for faster JavaScript.<br />

On each iteration <strong>of</strong> the loop we create a new element and set its src to point to the<br />

correct image file using a combination <strong>of</strong> the strings we created earlier and the counter<br />

variable. We set the z-index <strong>of</strong> each image as it is created using the css() method and the<br />

x variable used to control the loop. On each iteration, x will decrease, so each image added<br />

to the page will be lower down in stacking order than the previous one, which is exactly the<br />

order we require. We then append the image to our new container .<br />

At the end <strong>of</strong> each iteration, we increment the counter variable by 1. After the loop has<br />

completed, we append our container element, which now contains all <strong>of</strong> the necessary<br />

images, to the container hardcoded into the page. This will overlay the loading spinner.<br />

In a full implementation, we'd probably remove the spinner at this point.<br />

Pop quiz – implementing stop-motion animation with <strong>jQuery</strong><br />

1. In this example, we used a decrementing for loop, why?<br />

a. We need to in order to set a descending z-index on the images.<br />

b. The decrementing format <strong>of</strong> the loop is required when creating inline<br />

elements with <strong>jQuery</strong>.<br />

c. Because the code is easier to read.<br />

d. For performance reasons. Because the loop isn't checking a condition on<br />

every iteration. It's simply removing one from the value <strong>of</strong> x, so it runs faster.<br />

Have a go hero – extending stop-motion animation<br />

Simple two dimensional stickmen aren't the only images that can be used to create a<br />

stop-motion animation. Pretty much any series <strong>of</strong> sequential images can be used, so<br />

experiment with color images or photographs. Time-lapse photography <strong>of</strong>fers an<br />

excellent source <strong>of</strong> the right kind <strong>of</strong> photos to use.<br />

[ 204 ]

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

Saved successfully!

Ooh no, something went wrong!