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

Pop quiz – using show/hide logic 1. How are supported arguments passed to an effect? a. In string format as the second argument, for example show("blind", "vertical") [ 173 ] Chapter 6 b. As values in a configuration object passed directly to the animate() method, for example animate({ effect: "blind", configuration: { direction: "vertical" }) c. As values in a configuration object passed as the second argument, for example show("blind", { direction: "vertical" }) d. By setting the effect.config global property, for example $.effect. config = { direction: "vertical" }) 2. What else can be passed to the method? a. An integer or string representing the duration, and a callback function or function reference b. Nothing c. A Boolean that controls whether the animation should repeat indefinitely d. A Boolean indicating whether further effects should be queued or executed in parallel Have a go hero – experimenting with the effect API I would strongly recommend that you experiment with the effects that we have looked at in this section to see which ones work well with the effect() method and which ones work best with show/hide logic, and so you can see exactly what happens when the ones that don't work so well are used. This should improve your ability to quickly decide exactly when and where each method is appropriate. Easing functions To use the full set of easing functions with jQuery, we needed to use an extra plugin, but with jQuery UI all of the functions are included directly in the core effects file, so we can use them natively with any of our jQuery UI effects. Easing can be used with all of the jQuery UI effects with the exception of explode, although it can look a little strange in a few of the effects, such as bounce or pulsate. Easing can also be used if jQuery UI is present with standard jQuery.

Extended Animations with jQuery UI For a full list of the easing functions available with jQuery (either via the easing plugin, or jQuery UI) see the table in the easing section in Chapter 4, Sliding Animations. Time for action – adding easing to effects To use easing, all we need to do is include the easing function name as a configuration option. For example, to add easing to the blind.html example that we looked at earlier, we could change the JavaScript so that it appeared as follows: $("#window").click(function() { $("#blind").toggle("blind", { easing: "easeOutBounce" }); }); What just happened? We use the configuration option easing, with the name of the easing function as a string supplied as the value of the option. Any of the easing functions can be used by referencing their name in this way. Color animations As well as the complete range of easing functions, the effects core file also gives us the ability to attractively and smoothly animate between different colors. Several CSS properties can be animated including the color, background-color, border-color, and outlinecolor. jQuery UI extends jQuery's animate() method to achieve color animations, so the syntax to implement it is the same as using animate(). For any other purpose, we just need to target one of the above CSS properties and supply the new color value as a string, hexadecimal (#xxxxxx), or RGB (rgb(xxx, xxx, xxx)) value. Let's look at a basic example. Time for action – animating between colors In this example, we'll use color animations to show that a form field has been left empty. 1. In a fresh copy of the template file, use the following elements in the of the page: Search [ 174 ]

Extended <strong>Animation</strong>s with <strong>jQuery</strong> UI<br />

For a full list <strong>of</strong> the easing functions available with <strong>jQuery</strong> (either via the<br />

easing plugin, or <strong>jQuery</strong> UI) see the table in the easing section in Chapter 4,<br />

Sliding <strong>Animation</strong>s.<br />

Time for action – adding easing to effects<br />

To use easing, all we need to do is include the easing function name as a configuration<br />

option. For example, to add easing to the blind.html example that we looked at earlier,<br />

we could change the JavaScript so that it appeared as follows:<br />

$("#window").click(function() {<br />

$("#blind").toggle("blind", { easing: "easeOutBounce" });<br />

});<br />

What just happened?<br />

We use the configuration option easing, with the name <strong>of</strong> the easing function as a string<br />

supplied as the value <strong>of</strong> the option. Any <strong>of</strong> the easing functions can be used by referencing<br />

their name in this way.<br />

Color animations<br />

As well as the complete range <strong>of</strong> easing functions, the effects core file also gives us the ability<br />

to attractively and smoothly animate between different colors. Several CSS properties can<br />

be animated including the color, background-color, border-color, and outlinecolor.<br />

<strong>jQuery</strong> UI extends <strong>jQuery</strong>'s animate() method to achieve color animations, so the syntax to<br />

implement it is the same as using animate(). For any other purpose, we just need to target<br />

one <strong>of</strong> the above CSS properties and supply the new color value as a string, hexadecimal<br />

(#xxxxxx), or RGB (rgb(xxx, xxx, xxx)) value. Let's look at a basic example.<br />

Time for action – animating between colors<br />

In this example, we'll use color animations to show that a form field has been left empty.<br />

1. In a fresh copy <strong>of</strong> the template file, use the following elements in the <strong>of</strong> the<br />

page:<br />

Search<br />

[ 174 ]

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

Saved successfully!

Ooh no, something went wrong!