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.

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

The second section <strong>of</strong> code is encompassed within an anonymous function passed to <strong>jQuery</strong>'s<br />

each() method which we use to process each <strong>of</strong> the links in the header. The function we<br />

define is automatically passed an index (i) as an argument representing the current iteration<br />

which we use to build an ID string ready for populating the positions object.<br />

This object will contain a set <strong>of</strong> nested objects where each nested object represents one<br />

<strong>of</strong> the external pages and has a left and a top property which correspond to where on the<br />

single page the content is positioned.<br />

Working out where to place the content section on the vertical axis is easy; we just use the<br />

JavaScript modulus operator (%) to see if there is a remainder left after dividing the index by<br />

two, in conjunction with a ternary conditional.<br />

If the index can be divided by two without a remainder, we position the content from the<br />

external page one window's height from the top <strong>of</strong> the page. If there is a remainder, we<br />

just position it at the top <strong>of</strong> the page. This means that the content sections will be laid out in<br />

a zig-zag pattern along two rows, where each row is equal to the height <strong>of</strong> the window.<br />

Working out where to place each content section along the page horizontally is a little more<br />

challenging, but it's only the very last section that proves to be tricky. We use the ternary<br />

here as well, this time checking whether we are processing the last link or not.<br />

If we aren't, we simply position the content by multiplying the width <strong>of</strong> the window by the<br />

index, moving each successive section along the page by one window's width.<br />

If we are processing the last link however, we need to position the content by multiplying<br />

the width by the window width, but then subtracting one window's width dived by two,<br />

minus the width <strong>of</strong> the content's container dived by two. This ensures that the content<br />

section is aligned with the header and footer correctly.<br />

Once the location for the page that the current link points to has been added to the<br />

position's object, we then create the new container for the page content and give it a class<br />

name for styling purposes.<br />

We also use the load() method to load the external pages asynchronously. This method<br />

accepts the URL <strong>of</strong> the page to load, which we can get from the href property <strong>of</strong> the current<br />

link and a selector that matches all child elements within the content element in the page<br />

that is loaded. When a selector is passed to the load() method, only that portion <strong>of</strong> the<br />

external page will be retrieved.<br />

Once the container has been created, we position it using the css() method, setting<br />

its left and top properties to the corresponding properties in our positions object for<br />

convenience. Finally we append the new to the page's (which still only exists<br />

in memory at this point).<br />

[ 198 ]

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

Saved successfully!

Ooh no, something went wrong!