Hour 18. Using jQuery UI Effects


What You’ll Learn in This Hour:

Image Methods to apply effects using the jQuery UI library

Image How easing effects make animation changes variable

Image Ways to add cool effects when hiding/showing elements

Image How to apply effects to class changes

Image Applying animation effects when repositioning elements


jQuery UI provides a rich set of animation-type effects that can be applied to elements on your web pages. There are a couple of reasons why animating elements is a good thing. One is to leave the user with an impression that the website is interactive and fun to use. The second is to help users understand the visual changes that are taking place in your dynamic scripts.

In this hour, you see the improvements that jQuery UI provides in animation effects. You will be able to apply effects directly to elements, or you can apply effects when making class, visibility, or position changes.

Applying jQuery UI Effects

The purpose of this section is to introduce you to the effects that jQuery UI provides. This section discusses each of the effects and how to apply them using the .each() method. You are also introduced to the multitude of easing functions that provide a variable aspect to how values are applied during the effect animation.

Understanding jQuery UI Effects

jQuery UI effects are just animations to CSS position, size, and visibility properties. The animated changes are implemented in such a way as to create visual effects that give users a better experience.

For example, suppose a user tries to log in with an invalid password. In addition to the form validation message, you can also use jQuery UI effects to make the login button shake, which will catch the user’s attention better letting the user know the login failed. These are subtle changes to the web page, but they can have a large impact on the user experience.

Table 18.1 lists the effects with values that can be applied to manipulate them. This should give you an idea of the effects possible with jQuery UI. You implement some of these effects later in this hour.

Image
Image

TABLE 18.1 jQuery UI Effects

Setting the Effect Animation Easing

The easing function sets a value path that the effect uses when animating the effect. You have already seen the linear and swing easing in jQuery animations. jQuery UI adds a large number of new easing functions that can provide some fun animation effects.

The simplest way to illustrate how easing works is to show you the graphs published at the following location and shown in Figure 18.1. Think of the horizontal axis of the graphs as duration time, where left is 0 and the right is complete. Think of the vertical axis of the graph as how complete the transition of the effect is. For example, in a fade-out transition, the bottom would be fully opaque and the top would be fully transparent: http://api.jqueryui.com/easings/

Image

FIGURE 18.1 jQuery UI easing functions.

Adding Effects to jQuery Objects

There are multiple ways to apply effects to jQuery objects. Effects can be added as a part of another transition, such as a class change or visibility change. You can also apply effects to an element using the .effect() method. The .effect() method has the following syntax:

.effect( effect [, options ] [, duration ] [, complete ] )

In the .effect() method, effect is the name of the effect and options is an object containing the option values. Table 18.1 lists the effect names and options that you can apply to each effect. The duration is specified in milliseconds, and you can add an optional complete handler function that will be executed when the effect has been applied.

The following example illustrates the full syntax of applying a size effect to an element:

("img").effect("size",
               {to:{height:100, width:100}, origin:["right","top"], scale:"box"},
               3000,
               function(){alert("effect complete");});

Adding Effects to Class Transitions

A very important part of jQuery UI effects is the capability to animate transitions when applying classes to elements. This is done by adding a duration to the class transition function and specifying the easing function to control the animation effect. Any numerical class values that are changing will be animated each step of the class transition.


Note

Colors can be tricky; jQuery UI is not able to animate the transition from red to blue, but it can animate the transition from #FF0000 to #0000FF. If you want to animate color transitions, use the hex numerical value for them.


The following is a list of the different class transition methods that you can use to apply effects on jQuery objects by setting duration and easing values:

Image .addClass( className [, duration ] [, easing ] [, complete ] )—Adds the class and animates the changes to numerical class properties.

Image .removeClass( className [, duration ] [, easing ] [, complete ] ) )—Removes the class and animates the changes to numerical class properties.

Image .switchClass( removeClassName, addClassName [, duration ] [, easing ] [, complete ] ) )—First removes the removeClassName and animates the changes to numerical class properties, then adds the addClassName animating the numerical class property changes.

Image .toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )—Adds the class if the object(s) do not already have it or removes it if they do. Any changes to numerical class properties will be animated based on the easing function.

Adding Effects to Element Visibility Transitions

Another very cool effect that you can add to your web pages with jQuery UI is visibility changes. This can be one of the most useful in allowing users to visualize what is happening, and it provides them with a chance to follow the page flow better.

Visibility effects are applied in the same manner as the .effect() function you learned earlier in this hour. You specify an effect from Table 18.1 and then set the desired options, including an easing function if you want to control the animation.

The following is a list of the different element visibility transition methods that you can add effects to using jQuery UI:

Image .hide( effect [, options ] [, duration ] [, complete ] )—Applies the effect with options while hiding the element.

Image .show( effect [, options ] [, duration ] [, complete ] )—Applies the effect with options while showing the element.

Image .toggle( effect [, options ] [, duration ] [, complete ] )—Either shows or hides the object based on its current visibility and applies the specified effect while doing so.

Summary

jQuery UI effects are basically animations to the CSS properties of page elements. The benefit that they provide is that rather than having the effect happen instantaneously, you can have it happen gradually. Using easing functions, you can adjust the rate that the changes occur in the animation to give elements more of an interactive feel.

Q&A

Q. Is there a way to animate changing an <img> element from one source to another so that part of both elements are visible at the same time?

A. Not directly, but there is a trick you can employ. Use two <img> elements and animate the opacity property changes at the same time. As one disappears, the other one will become visible at the same time.

Q. Is there a way to create custom easing functions?

A. Yes, you can create a custom easing function and attach it to $.easing. The function needs to accept the following parameters and return a new value based on those parameters:

Image tPercent—Percentage of time passed in the animation from 0.0 to 1.0.

Image tMS—Milliseconds since animation started.

Image startValue—Starting value of the property.

Image endValue—Ending value of the property.

Image tTotal—Duration of the animation.

$.easing.myCustom = function(tPercent, tMS, startValue, endValue, tTotal) {
    var newValue= <your code here>>...
   return newValue;
}

Workshop

The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try to answer the questions before looking at the answers.

Quiz

1. How do you control the amount of time the effect will take?

2. How do you define the number of pieces the explode effect will generate to 25?

3. True or False: You cannot animate changes to the border-style.

4. What effect would you use to simulate an element shrinking?

Quiz Answers

1. Setting the duration value.

2. Set the options value to {pieces:25}.

3. True. You can only animate numerical changes.

4. Scale or Size.

Exercises

1. Open the code in Listing 18.1 and change which effects are applied to the images. Try applying the pulsate, drop, and puff effects.

2. Modify the code in Listing 18.4. Add a duration attribute to each of the coordinates so that you can also adjust the duration time for each point in the ball’s animation. Add a few new points, as well. The coordinate values should look something like this:

{top:20, left:300, easing:"easeInBounce", duration:1500},

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset