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.

Sliding <strong>Animation</strong>s<br />

The previous picture shows the menu as it opens. The animation proceeds from top to<br />

bottom. Because the slideDown() effect is only applied to elements that are hidden,<br />

clicking the link while the menu is open will not cause the menu to reopen.<br />

What just happened?<br />

We have a relatively straight-forward collection <strong>of</strong> elements here. The HTML5 <br />

element is the natural container for our login elements. The form is part <strong>of</strong> a simple<br />

unordered-list along with a link that will open the form.<br />

Note that the link that will be used to open the login form has its href attribute set to<br />

a page that contains the login form. This is a simple fallback so that if the visitors have<br />

JavaScript disabled, they can still login even if they don't get to see the slide down form.<br />

A lot <strong>of</strong> the styling is purely presentational and does not affect how the form functions.<br />

Similarly, a lot <strong>of</strong> it is standard positional stuff. What's important is that the form is initially<br />

hidden from view with display:none, and that the form is positioned using its top<br />

style property.<br />

We attach a click-handler to the login link, which automatically receives the event object<br />

(e) as an argument. We use this object to cancel the default behavior <strong>of</strong> the browser. This<br />

prevents the page from jumping to the top when the link is clicked, or following the href<br />

<strong>of</strong> the link provided in case JavaScript is disabled. The preventDefault() function, which<br />

<strong>jQuery</strong> normalizes to work in IE as well as standard-compliant browsers, handles this for us.<br />

We then add the class name on to the link so that our :hover styles are persisted when<br />

the pointer moves <strong>of</strong>f the link and the form is still open. We then move to the next element<br />

after the link, our hidden login form, and call the slideDown() method on it with no<br />

additional arguments.<br />

Position is important<br />

The position <strong>of</strong> an element that has slideDown() applied to it is very<br />

important. The animation will not run correctly if the element uses absolute<br />

positioning in conjunction with its bottom style property to position itself by.<br />

In this situation, the element will appear to slide up from the bottom, instead<br />

<strong>of</strong> sliding down as intended. We do not see this behavior with relatively<br />

positioned elements however.<br />

[ 78 ]

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

Saved successfully!

Ooh no, something went wrong!