jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
Download from Wow! eBook }, timer = setInterval(function() { removeFrame() }, 50); for(var x = 75; x--;) { $("", { src: srcStr1 + counter + srcStr2 }).css("zIndex", x).appendTo(frames); counter++; } frames.appendTo("#cartoon"); When we run the page, the animation should proceed as we expect, much like the type of sketch we perhaps may have idly created in a notepad in our youth and "watched" by flicking through the pages quickly. The following screenshot shows a single frame of the stickman animation: Clearly, the best way to view the animation is in a browser. [ 203 ] Chapter 7 What just happened? We start out by initializing some variables. We set a counter variable and a series of strings representing the common strings that we'll need to use repeatedly. These will be used inside a for loop so we don't want to define them within the loop as JavaScript will create the same string objects repeatedly, whereas by defining them outside of the loop will ensure they only get created once. We also create a new container which we'll append each of the new frames to, and assign a function to the setInterval JavaScript function.
Full Page Animations Next we define the removeFrame() function which will be executed by setInterval. All this function does is check whether there is more than one element within the frames container and if so, remove the first one. Every 50 milliseconds, the top image will be removed, which is fast enough for the repeated showing of still images to be perceived as an animation. If there is only one image left, we clear the timeout as the animation has completed. Next we define the for loop, specifying the maximum number of frames, and decrementing on each iteration of the loop. We don't need to specify a comparison condition in this form of loop however, because the loop will naturally end when x = 0 (because 0 is a falsey value). Using decrementing for loops is a proven strategy for faster JavaScript. On each iteration of the loop we create a new element and set its src to point to the correct image file using a combination of the strings we created earlier and the counter variable. We set the z-index of each image as it is created using the css() method and the x variable used to control the loop. On each iteration, x will decrease, so each image added to the page will be lower down in stacking order than the previous one, which is exactly the order we require. We then append the image to our new container . At the end of each iteration, we increment the counter variable by 1. After the loop has completed, we append our container element, which now contains all of the necessary images, to the container hardcoded into the page. This will overlay the loading spinner. In a full implementation, we'd probably remove the spinner at this point. Pop quiz – implementing stop-motion animation with jQuery 1. In this example, we used a decrementing for loop, why? a. We need to in order to set a descending z-index on the images. b. The decrementing format of the loop is required when creating inline elements with jQuery. c. Because the code is easier to read. d. For performance reasons. Because the loop isn't checking a condition on every iteration. It's simply removing one from the value of x, so it runs faster. Have a go hero – extending stop-motion animation Simple two dimensional stickmen aren't the only images that can be used to create a stop-motion animation. Pretty much any series of sequential images can be used, so experiment with color images or photographs. Time-lapse photography offers an excellent source of the right kind of photos to use. [ 204 ]
- Page 173 and 174: Extended Animations with jQuery UI
- Page 175 and 176: Extended Animations with jQuery UI
- Page 177 and 178: Extended Animations with jQuery UI
- Page 179 and 180: Extended Animations with jQuery UI
- Page 181 and 182: Extended Animations with jQuery UI
- Page 183 and 184: Extended Animations with jQuery UI
- Page 185 and 186: Extended Animations with jQuery UI
- Page 187 and 188: Extended Animations with jQuery UI
- Page 189 and 190: Extended Animations with jQuery UI
- Page 191 and 192: Extended Animations with jQuery UI
- Page 193 and 194: Extended Animations with jQuery UI
- Page 195 and 196: Extended Animations with jQuery UI
- Page 197 and 198: Extended Animations with jQuery UI
- Page 199 and 200: Extended Animations with jQuery UI
- Page 201 and 202: Full Page Animations Can we animate
- Page 203 and 204: Full Page Animations display:block;
- Page 205 and 206: Download from Wow! eBook Full Page
- Page 207 and 208: Full Page Animations Have a go hero
- Page 209 and 210: Full Page Animations 4. Save this f
- Page 211 and 212: Full Page Animations 2. The bgpos c
- Page 213 and 214: Full Page Animations nav ul { margi
- Page 215 and 216: Full Page Animations screensize = {
- Page 217 and 218: Full Page Animations What just happ
- Page 219 and 220: Full Page Animations The second sec
- Page 221 and 222: Full Page Animations Pop quiz - cre
- Page 223: Full Page Animations Time for actio
- Page 228 and 229: 8 Other Popular Animations This cha
- Page 230 and 231: [ 209 ] Chapter 8 } .slider #scroll
- Page 232 and 233: }), pointerText = "Use your pointer
- Page 234 and 235: [ 213 ] Chapter 8 In the previous s
- Page 236 and 237: prox.mousemove(function(ev) { goAni
- Page 238 and 239: Pop quiz - implementing proximity a
- Page 240 and 241: [ 219 ] Chapter 8 What just happene
- Page 242 and 243: 2. Save the new page as marquee.htm
- Page 244 and 245: [ 223 ] Chapter 8 1. jQuery provide
- Page 246 and 247: clear:both; } div#fadeLeft, div#fad
- Page 248 and 249: Time for action - animating the pos
- Page 250: 2. In this example we used the dele
- Page 253 and 254: CSS3 Animations The following trans
- Page 255 and 256: CSS3 Animations Units It is of crit
- Page 257 and 258: CSS3 Animations Skew Remember the t
- Page 259 and 260: CSS3 Animations Calculating the sin
- Page 261 and 262: CSS3 Animations The filter property
- Page 263 and 264: CSS3 Animations If, like me, you th
- Page 265 and 266: CSS3 Animations } el.filters.item("
- Page 267 and 268: CSS3 Animations An unfortunate prob
- Page 269 and 270: CSS3 Animations The context of this
- Page 271 and 272: CSS3 Animations Time for action - i
- Page 273 and 274: CSS3 Animations The first variable
Download from Wow! eBook <br />
},<br />
timer = setInterval(function() { removeFrame() }, 50);<br />
for(var x = 75; x--;) {<br />
$("", {<br />
src: srcStr1 + counter + srcStr2<br />
}).css("z<strong>Index</strong>", x).appendTo(frames);<br />
counter++;<br />
}<br />
frames.appendTo("#cartoon");<br />
When we run the page, the animation should proceed as we expect, much like the type<br />
<strong>of</strong> sketch we perhaps may have idly created in a notepad in our youth and "watched" by<br />
flicking through the pages quickly. The following screenshot shows a single frame <strong>of</strong> the<br />
stickman animation:<br />
Clearly, the best way to view the animation is in a browser.<br />
[ 203 ]<br />
Chapter 7<br />
What just happened?<br />
We start out by initializing some variables. We set a counter variable and a series <strong>of</strong> strings<br />
representing the common strings that we'll need to use repeatedly. These will be used inside<br />
a for loop so we don't want to define them within the loop as JavaScript will create the<br />
same string objects repeatedly, whereas by defining them outside <strong>of</strong> the loop will ensure<br />
they only get created once.<br />
We also create a new container which we'll append each <strong>of</strong> the new frames to, and<br />
assign a function to the setInterval JavaScript function.