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

[ 147 ] Chapter 6 What just happened? When a Delete link is clicked, our handler function first sets the background-color of the that the link is within. This is not mandatory for the effect, but it does help bring it to life. We then apply the pulsate effect to all elements within the row using the effect() method. We need to apply the effect to the elements instead of the element so that the effect works as intended in IE. When the effect ends, our inline callback function will be executed which removes the . Obviously the can only be removed once, but once it has been removed, subsequent attempts to remove it will just fail silently. The shake effect The shake effect shakes the element that it is applied to back and forth a specified number of times. Configuration options The shake effect exposes three configuration options that allow us to customize its behavior. These are listed in the following table: Option Default Usage direction "left" Sets the direction that the element moves in distance 20 Sets the number of pixels the element travels when it is shaken times 3 Sets the number of times the element shakes Time for action – shaking an element The open source .Net CMS Umbraco uses the shake effect when incorrect login details are entered in the sign-in form for its back-office administration area. In this example we can see how easy it is to implement this behavior using the shake effect. 1. Add the following markup to the template file as the basis of the log in form: Login Username: Password: Login

Extended Animations with jQuery UI 2. Now add the following code to the empty closure at the bottom of the template file: $("#submit").click(function(e) { e.preventDefault(); $("input").each(function(i, val) { if (!$(this).val()) { $(this).css("border", "1px solid red").effect("shake", { distance: 5 }, 100); } }); }); 3. Save this file as shake.html. We also need a basic stylesheet for this example. Add the following CSS to a new file: form { width:145px; padding:20px; margin:auto; border:1px solid #000; font:normal 13px "Nimbus Sans L", "Helvetica Neue", "Franklin Gothic Medium", Sans-serif; } h2 { font-size:14px; margin-top:0; } input { display:block; margin-bottom:10px; border:1px solid #000; } 4. Save this file as shake.css. 5. If we run the page in a browser and click the without completing either of the fields, both fields will have their borders set to red and will shake from side to side: [ 148 ]

[ 147 ]<br />

Chapter 6<br />

What just happened?<br />

When a Delete link is clicked, our handler function first sets the background-color <strong>of</strong><br />

the that the link is within. This is not mandatory for the effect, but it does help bring<br />

it to life.<br />

We then apply the pulsate effect to all elements within the row using the effect()<br />

method. We need to apply the effect to the elements instead <strong>of</strong> the element so<br />

that the effect works as intended in IE.<br />

When the effect ends, our inline callback function will be executed which removes the .<br />

Obviously the can only be removed once, but once it has been removed, subsequent<br />

attempts to remove it will just fail silently.<br />

The shake effect<br />

The shake effect shakes the element that it is applied to back and forth a specified number<br />

<strong>of</strong> times.<br />

Configuration options<br />

The shake effect exposes three configuration options that allow us to customize its behavior.<br />

These are listed in the following table:<br />

Option Default Usage<br />

direction "left" Sets the direction that the element moves in<br />

distance 20 Sets the number <strong>of</strong> pixels the element travels when<br />

it is shaken<br />

times 3 Sets the number <strong>of</strong> times the element shakes<br />

Time for action – shaking an element<br />

The open source .Net CMS Umbraco uses the shake effect when incorrect login details are<br />

entered in the sign-in form for its back-<strong>of</strong>fice administration area. In this example we can see<br />

how easy it is to implement this behavior using the shake effect.<br />

1. Add the following markup to the template file as the basis <strong>of</strong> the log in form:<br />

<br />

Login<br />

Username:<br />

Password:<br />

Login<br />

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

Saved successfully!

Ooh no, something went wrong!