concept effect() in category jQuery

appears as: effect()
jQuery UI in Action

This is an excerpt from Manning's book jQuery UI in Action.

jQuery UI includes 15 built-in animations, that provide ways to show and hide elements, draw the user’s attention to elements, or add visual appeal to your UIs. These effects stand on their own with the effect()method, tie into existing widgets such as dialog, and work with jQuery Core methods such as show() and hide(). But it doesn’t stop there. jQuery UI also adds powerful abilities to animate CSS class name changes, transition between colors, and a whole lot more.

The most common—and easiest—way to run the jQuery UI effects is through the effect() plugin method. In its simplest form, you pass the effect() plugin the name of the effect to use. The following code shakes a blue box:

The values listed in bold—direction, distance, and times—are options that configure the effect. To pass these options, provide them as objects as the second argument to effect(). The following code alters the previous example to shake the blue box 10 times over a distance of 100 pixels:

$( "div" ).effect( "shake", {
    times: 10,
    distance: 100
});

If you test this example, the effect runs so fast that it looks like a blur. This is because the next parameter of effect()—duration—defaults to 400 milliseconds, which isn’t nearly enough time to shake a box 10 times. The following code increases the duration to 3 full seconds:

Now your animation has plenty of time to do its shaking. The final argument of the effect() plugin is a function that runs as a callback when the animation completes. The following code adds a callback to your example that makes the box red after the shaking finishes:

$( "div" ).effect( "shake", {
    times: 10,
    distance: 100
}, 3000, function() {
    $( this ).css( "background", "red" );
});

Although this is powerful, the API is starting to feel messy; the previous code certainly isn’t clear to read. Because of this, the effect() plugin offers an alternative signature in which all arguments are passed as a single object. The following code alters your example to use the object signature:

Take note of two changes here. First, the name of the effect to use is passed as an effect property . This is the only required property when using the effect() object signature. The other change is the properties that apply only to the shake effect (times and distance) are no longer in a separate “options” argument; they’re included directly in the object passed to effect().

The object form of effect() takes one additional property we haven’t discussed: an easing.