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.

[ 183 ]<br />

Chapter 7<br />

What just happened?<br />

The HTML is very simple. There's just more <strong>of</strong> it than there has been in previous examples.<br />

It represents a single article that is divided into different sections, with each section having a<br />

sub-heading and some layout text.<br />

A table <strong>of</strong> contents style navigation menu at the top <strong>of</strong> the article (this would be considered<br />

a secondary nav) links to each <strong>of</strong> the different sections. The styling is also very basic and<br />

simply sets some rules to lay out the page in a clean format.<br />

Time for action – animating the scroll<br />

The script itself is relatively straight-forward too. Add the following code to the empty<br />

function at the bottom <strong>of</strong> the HTML file:<br />

var parent = document.getElementById("post"),<br />

speed = 7,<br />

win = $(window);<br />

$("nav a", parent).click(function(e) {<br />

e.preventDefault();<br />

var target = $(this).attr("href"),<br />

<strong>of</strong>fset = $(target).<strong>of</strong>fset(),<br />

newScroll = 0,<br />

maxScroll = document.body.scrollHeight;<br />

}<br />

});<br />

while (newScroll < <strong>of</strong>fset.top && win.scrollTop() < maxScroll) {<br />

win.scrollTop(newScroll);<br />

newScroll = newScroll + speed;<br />

$(".top", parent).click(function(e) {<br />

}<br />

});<br />

e.preventDefault;<br />

var newScroll = win.scrollTop();<br />

while (newScroll > 0 && win.scrollTop() > 0) {<br />

win.scrollTop(newScroll);<br />

newScroll = newScroll - speed;

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

Saved successfully!

Ooh no, something went wrong!