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

Time for action – creating the dialog [ 29 ] Chapter 2 We'll start again by creating the underlying markup that the dialog is built with and add any necessary styling. 1. First, add the following code to the in a fresh copy of the template file: A dialog box of some description Lorem ipsum etc, etc. Ok 2. We'll also need to link to a stylesheet in the of the page: 3. Save this page as fadeOut.html in the main project folder. The code for the stylesheet is as follows: #dialog { background-color:#fff; width:300px; padding:10px; font:normal 14px "Nimbus Sans L", "Helvetica Neue", "Franklin Gothic Medium", Sans-serif; border:1px solid #aaa; -moz-border-radius:7px; -webkit-border-radius:7px; border-radius:7px; -moz-box-shadow:2px 2px 5px #000; -webkit-box-shadow:2px 2px 5px #000; box-shadow:2px 2px 5px #000; } #dialog:after { content:""; height:0; display:block; clear:both; visibility:hidden; } #dialog header { display:block; padding-bottom:5px; margin-bottom:10px; font-weight:bold; font-size:16px; border-bottom:1px solid #aaa; } #dialog footer { display:block; padding-top:10px; margin-top:10px; border-top:1px solid #aaa; } #dialog button { float:right; }

Fading Animations 4. Save this in the css folder as fadeOut.css. To perform the fadeOut() animation, we'll need just a tiny bit of JavaScript. Inside the anonymous function in the second element at the bottom of the , add the following code: $("#overlay button").click(function() { $("#overlay").fadeOut(500, function() { $(this).remove(); }); }); What just happened? In our code, we attach a click handler to the using jQuery's click() helper method. The anonymous function we specify as an argument is executed whenever the button is clicked. Within this function we select the dialog using its id and call the fadeOut() method on it using a numerical argument of 500 milliseconds instead of one of the acceptable strings. We also specify a callback function as the second argument to the fadeOut() method. As there is only a single element in the selection (the element with an id of overlay), this function will be executed only once. Inside this callback function, the this keyword is set to the current element, so we can easily manipulate it from within the callback. All the function does is remove the dialog from the DOM. This behavior would be appropriate when the element to be removed was a one-time-only dialog that would either not be shown again during the current session, or would be generated again from scratch by the system when required. We'll see the dialog when we load the page. In a proper implementation, it would probably be centered in the viewport and be absolutely positioned so that it appears to float above the page (additionally the CSS3 shadow that we used would reinforce this impression). It would also more than likely be modal, so the underlying page would be obscured, or otherwise shielded from interaction, until the dialog is closed. To avoid unnecessary cluttering of the example however, our dialog is alone on an empty page. Clicking the will cause the dialog to fade away and then be removed from the page. The animation is fairly quick and less jarring than instantaneous removal of the dialog, but I should point out that fading animations can often annoy users if they are too frequent, take too long to complete, or are felt to be completely unnecessary. [ 30 ]

Time for action – creating the dialog<br />

[ 29 ]<br />

Chapter 2<br />

We'll start again by creating the underlying markup that the dialog is built with and add any<br />

necessary styling.<br />

1. First, add the following code to the in a fresh copy <strong>of</strong> the template file:<br />

<br />

A dialog box <strong>of</strong> some description<br />

Lorem ipsum etc, etc.<br />

Ok<br />

<br />

2. We'll also need to link to a stylesheet in the <strong>of</strong> the page:<br />

<br />

3. Save this page as fadeOut.html in the main project folder. The code for the<br />

stylesheet is as follows:<br />

#dialog {<br />

background-color:#fff; width:300px; padding:10px;<br />

font:normal 14px "Nimbus Sans L", "Helvetica Neue", "Franklin<br />

Gothic Medium", Sans-serif;<br />

border:1px solid #aaa; -moz-border-radius:7px;<br />

-webkit-border-radius:7px; border-radius:7px;<br />

-moz-box-shadow:2px 2px 5px #000;<br />

-webkit-box-shadow:2px 2px 5px #000;<br />

box-shadow:2px 2px 5px #000;<br />

}<br />

#dialog:after {<br />

content:""; height:0; display:block; clear:both;<br />

visibility:hidden;<br />

}<br />

#dialog header {<br />

display:block; padding-bottom:5px; margin-bottom:10px;<br />

font-weight:bold; font-size:16px; border-bottom:1px solid #aaa;<br />

}<br />

#dialog footer {<br />

display:block; padding-top:10px; margin-top:10px;<br />

border-top:1px solid #aaa;<br />

}<br />

#dialog button { float:right; }

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

Saved successfully!

Ooh no, something went wrong!