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.

[ 185 ]<br />

Chapter 7<br />

Within the while loop, we use <strong>jQuery</strong>'s scrollTop() method in setter mode to set<br />

the scroll to the value <strong>of</strong> the newScroll variable, and then increment the newScroll<br />

value using our speed variable. This is what I meant by spo<strong>of</strong>ing the animated scroll—the<br />

document just scrolls sequentially in a series <strong>of</strong> jumps; it isn't actually animated, but because<br />

it happens fairly rapidly it gives the impression <strong>of</strong> being animated.<br />

After the click handler for the table <strong>of</strong> contents links, we also set a click handler on the<br />

back to top links. Because we're just going straight back to the top <strong>of</strong> the page, we don't<br />

need to do any complex calculations, so this function is really just a simplified version <strong>of</strong><br />

the first function.<br />

There is already a <strong>jQuery</strong> plugin that can be used to animate scroll: the scrollTo<br />

plugin. This plugin allows us to easily animate both vertical and horizontal scroll<br />

on any element whose contents overflows the dimensions set on it.<br />

However, as an exercise in how to fake animation without using any <strong>of</strong> <strong>jQuery</strong>'s<br />

animation methods, I thought it would be <strong>of</strong> value to do it ourselves manually<br />

here. We'll use the plugin later in the chapter. For reference it was created by<br />

Ariel Flesler and can be downloaded from http://flesler.blogspot.<br />

com/2007/10/jqueryscrollto.html.<br />

Pop quiz – animating page scroll<br />

1. In this example we used old-school JavaScript to obtain a reference to the<br />

DOM node instead <strong>of</strong> using <strong>jQuery</strong>. Why?<br />

a. Because it's easier<br />

b. Because it's fun to mix things up a little<br />

c. Because it's faster to give <strong>jQuery</strong> selectors a DOM node context so that the<br />

entire document doesn't need to be searched when selecting elements from<br />

the page<br />

d. Because it makes the byte count <strong>of</strong> the page smaller<br />

2. Why can we not use <strong>jQuery</strong> to animate the page scroll?<br />

a. Because scrollTop (or window.pageYOffset) are properties <strong>of</strong> the<br />

document or window and not CSS style properties<br />

b. Because the values for these properties are not true integers<br />

c. Because <strong>jQuery</strong> does not want us to animate scroll<br />

d. We can, we just chose to do it this way instead

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

Saved successfully!

Ooh no, something went wrong!