Jump to content

How to use jQuery UI for advanced effects

0
  adfm's Photo
Posted Feb 16 2010 02:05 PM

Get past the simple effects you can easily generate with jQuery. In this excerpt from jQuery Cookbook the authors will demonstrate the use of jQuery UI for advanced effects.


If you want to create more complicated effects, it is certainly possible using the animate method. This might be for a web application that needs to animate a whole range of CSS properties on an element, or perhaps there is a special way a dialog box must disappear when closed—say, for instance, explode away on the screen (see Figure 7.2).

Figure 7.2. The explode effect running against the div.box element

Attached Image

Download the jQuery UI library from http://jquery-cookbo...ueryui-download. The library can now be included after jQuery is included and the new effects plugin is available.

For this solution, I have added an extra button to show two effects and added a new class to our CSS.

CSS


.big {

 font-size: 400%;

 width: 500px;

 height: 500px;

 line-height: 100%;

}

jQuery


$(document).ready(function () {

 $('#animate').click(function () {

	$('.box').toggleClass('big', 2000);

 });



 $('#effect').click(function () {

	$('.box').effect('explode', null, 2000);

 });

});

Discussion

The jQuery UI effects library also modifies the way addClass, removeClass, and toggleClass work; in particular, you can supply a duration as the second parameter, and it will animate a transition from the current state to the new class, working through all new class properties.

So, the first example adds the class big and sets the animation to run for two seconds. All the CSS properties from the big class are animated onto the div.box element. Because the toggleClass method has also been modified by jQuery UI, we are able to toggle back and forth to the original state.

Second, we are using the effect() method, which is bespoke to the jQuery UI library. This method offers a collection of show and hide functions.

Note

The effect() method requires the option object passed in as the second variable; this can be null or it can be an empty object, but it must be provided to be able to pass in the duration.

Using the string explode, the div.box will split into nine pieces and fade off the page as shown earlier in Figure 7.2.

Warning

At the time of this writing, one or two effect types have slight side effects in Safari 4. They do work in all other A-grade browsers as outlined by Yahoo! at http://developer.yah...i/articles/gbs/.

To see all the different available effects, you can visit http://jquery-cookbo...queryui-effects and play with all the interactive demos.

Cover of jQuery Cookbook
Learn more about this topic from jQuery Cookbook. 

Getting started with the jQuery library is easy, but it can take years to fully realize its breadth and depth; jQuery Cookbook shortens the learning curve considerably. You'll learn patterns and practices from 19 leading developers who use jQuery for everything from integrating simple components into websites and applications to developing complex, high-performance user interfaces. The recipes start with the basics and then move into practical use cases with tested solutions to common web development hurdles.

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies