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.

Download from Wow! eBook <br />

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

Save the HTML file again and run it in a browser. You should find that the document<br />

smoothly scrolls to the desired location when any <strong>of</strong> the top links on the page are clicked.<br />

What just happened?<br />

First we set some variables including a reference to the DOM node <strong>of</strong> the parent container<br />

(the ). We obtain this with the raw JavaScript getElementById() function so<br />

that we can pass it into a <strong>jQuery</strong> selector as a context for the selector.<br />

Doing this is great for performance and means that we don't have to give multiple elements<br />

id attributes for the sole purpose <strong>of</strong> <strong>jQuery</strong> element selection. Everything is selected by<br />

starting at the parent that is originally selected from the document.<br />

We also set a variable called speed which we'll use when we create the simulated<br />

animation. This is the figure that the current scroll amount is incremented by, so lower<br />

values will mean a longer "animation" time and higher values will mean a shorter time,<br />

completely the opposite <strong>of</strong> <strong>jQuery</strong>'s duration argument.<br />

We also cache a selector for the window object as we'll need to refer to this several<br />

times, <strong>of</strong>ten from within for or while conditional branches, so again this is purely<br />

for performance reasons.<br />

We then set a click handler on the table <strong>of</strong> contents links. Within this function we first stop<br />

the browser from following the link using the preventDefault() method, which <strong>jQuery</strong><br />

normalizes across all common browsers.<br />

We then set some more variables, first getting the target element <strong>of</strong> the link that was<br />

clicked, then storing its current <strong>of</strong>fset, and the maximum scroll amount <strong>of</strong> the document.<br />

We also initialize a newScroll variable so that we can calculate what the new scroll amount<br />

should be.<br />

We then use a JavaScript while loop to incrementally scroll the document, using the speed<br />

variable that we set earlier. The condition <strong>of</strong> the loop is that it should continue while the<br />

newScroll variable is less than the top <strong>of</strong>fset <strong>of</strong> the target element and while the current<br />

scroll is less than the maximum scroll.<br />

The <strong>of</strong>fset <strong>of</strong> the target element is obtained using <strong>jQuery</strong>'s <strong>of</strong>fset() method, which returns<br />

an object with top and left properties that correspond to the element's position relative<br />

to the document. The maximum scroll is obtained using standard JavaScript to query the<br />

scrollHeight property <strong>of</strong> the document body.<br />

The current scroll is normalized by <strong>jQuery</strong> so that the scrollTop() method returns the<br />

current position <strong>of</strong> the vertical scroll bar. This is useful because it means that we don't<br />

have to feature-detect the browser and obtain the value using either document.body.<br />

scrollTop or window.pageYOffset depending on the browser in use.<br />

[ 184 ]

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

Saved successfully!

Ooh no, something went wrong!