jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

[ 199 ] Chapter 7 We then set the href of the current link to a document fragment identifier pointing to the name of the corresponding content section. This wouldn't have any effect if we weren't intercepting clicks on the nav links because the content sections don't have matching id attributes, but it is necessary to store the fragment here so that we can read it back when the link is clicked. Lastly, we check again whether we're processing the last link or not, and if we are, we trigger our custom contentLoaded event, which results in the page's element being appended to the page, and the click handlers bound to the navigation links. Building a site like this which pulls all of its content into a single page won't suit every type of site. Most clients would probably pay the cancellation fees and swiftly withdraw if this idea was presented to them. However, on highly stylized sites, where the design and behavior of the site is of special importance, this kind of effect can work well. Sites with little content on each page are especially suited to it. The following screenshot shows the functionality in action: In the previous screenshot, we can see the header and footer partly faded out, and the page content being scrolled diagonally downwards to the right.

Full Page Animations Pop quiz – creating a single-page website 1. We interact with the this object directly in this example instead of the jQuery equivalent $(this), why? a. Because it uses fewer characters b. Its faster and more efficient because an entirely new jQuery object is not created c. Because it looks better d. Because it contains more information 2. We create and use a custom event in this example, why? a. Because custom events execute faster than standard browser events b. Because the each() method does not allow us to update the scrollTop property of the window c. Because it is more efficient for the code executed by the handler to be called once than on every iteration of the each() method d. Because the scrollTo plugin can only be used in conjunction with custom events Have a go hero – extending single-page navigation There are several things you could do to expand on this example. One thing you could do is add functionality that checks which page is requested by looking at the href property of the document. If a page other than the first page is requested, you could scroll to the corresponding page section so that the page they link to is actually shown instead of resetting to the first page. Another thing you could do would be to extend the example so that the pages don't have to be numbered page-2.html, and so on, and instead could have any file name. In the first section of code, we read the href of the link if one of the footer links is clicked instead of looking for a document fragment identifier. This same technique could be applied to the header links as well, so that any page that is linked to can be included in the single page. Or, to really appreciate the visual effect of our site-on-a-page, you could add some additional content and a theme to the site. Each page need not have the same skin, and scrolling between different colors and imagery can really bring the page to life. [ 200 ]

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

Pop quiz – creating a single-page website<br />

1. We interact with the this object directly in this example instead <strong>of</strong> the <strong>jQuery</strong><br />

equivalent $(this), why?<br />

a. Because it uses fewer characters<br />

b. Its faster and more efficient because an entirely new <strong>jQuery</strong> object is not<br />

created<br />

c. Because it looks better<br />

d. Because it contains more information<br />

2. We create and use a custom event in this example, why?<br />

a. Because custom events execute faster than standard browser events<br />

b. Because the each() method does not allow us to update the scrollTop<br />

property <strong>of</strong> the window<br />

c. Because it is more efficient for the code executed by the handler to be called<br />

once than on every iteration <strong>of</strong> the each() method<br />

d. Because the scrollTo plugin can only be used in conjunction with<br />

custom events<br />

Have a go hero – extending single-page navigation<br />

There are several things you could do to expand on this example. One thing you could do<br />

is add functionality that checks which page is requested by looking at the href property<br />

<strong>of</strong> the document. If a page other than the first page is requested, you could scroll to the<br />

corresponding page section so that the page they link to is actually shown instead <strong>of</strong><br />

resetting to the first page.<br />

Another thing you could do would be to extend the example so that the pages don't have<br />

to be numbered page-2.html, and so on, and instead could have any file name. In the<br />

first section <strong>of</strong> code, we read the href <strong>of</strong> the link if one <strong>of</strong> the footer links is clicked instead<br />

<strong>of</strong> looking for a document fragment identifier. This same technique could be applied to the<br />

header links as well, so that any page that is linked to can be included in the single page.<br />

Or, to really appreciate the visual effect <strong>of</strong> our site-on-a-page, you could add some additional<br />

content and a theme to the site. Each page need not have the same skin, and scrolling<br />

between different colors and imagery can really bring the page to life.<br />

[ 200 ]

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

Saved successfully!

Ooh no, something went wrong!