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.

Fading <strong>Animation</strong>s<br />

4. Save this in the css folder as fadeOut.css. To perform the fadeOut() animation,<br />

we'll need just a tiny bit <strong>of</strong> JavaScript. Inside the anonymous function in the second<br />

element at the bottom <strong>of</strong> the , add the following code:<br />

$("#overlay button").click(function() {<br />

$("#overlay").fadeOut(500, function() {<br />

$(this).remove();<br />

});<br />

});<br />

What just happened?<br />

In our code, we attach a click handler to the using <strong>jQuery</strong>'s click() helper<br />

method. The anonymous function we specify as an argument is executed whenever<br />

the button is clicked. Within this function we select the dialog using its id and call the<br />

fadeOut() method on it using a numerical argument <strong>of</strong> 500 milliseconds instead <strong>of</strong> one<br />

<strong>of</strong> the acceptable strings.<br />

We also specify a callback function as the second argument to the fadeOut() method.<br />

As there is only a single element in the selection (the element with an id <strong>of</strong> overlay),<br />

this function will be executed only once. Inside this callback function, the this keyword<br />

is set to the current element, so we can easily manipulate it from within the callback.<br />

All the function does is remove the dialog from the DOM. This behavior would be<br />

appropriate when the element to be removed was a one-time-only dialog that would either<br />

not be shown again during the current session, or would be generated again from scratch by<br />

the system when required.<br />

We'll see the dialog when we load the page. In a proper implementation, it would probably<br />

be centered in the viewport and be absolutely positioned so that it appears to float above<br />

the page (additionally the CSS3 shadow that we used would reinforce this impression).<br />

It would also more than likely be modal, so the underlying page would be obscured, or<br />

otherwise shielded from interaction, until the dialog is closed.<br />

To avoid unnecessary cluttering <strong>of</strong> the example however, our dialog is alone on an empty<br />

page. Clicking the will cause the dialog to fade away and then be removed from<br />

the page.<br />

The animation is fairly quick and less jarring than instantaneous removal <strong>of</strong> the dialog,<br />

but I should point out that fading animations can <strong>of</strong>ten annoy users if they are too frequent,<br />

take too long to complete, or are felt to be completely unnecessary.<br />

[ 30 ]

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

Saved successfully!

Ooh no, something went wrong!