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

[ 91 ] Chapter 4 Easing is a great effect that can be added with the addition of an 8 KB plugin (3.51 KB when minified and with the license moved to an external file) and a very minor tweak to our code. Using it is simple, but its effects can be enormous, transforming a monotonous or otherwise boring animation into one filled with impact and interest. Using an object literal to add easing We can also change the format of the arguments we pass into the predefined animation methods in order to use easing. Prior to the easing argument being added to the animation methods (fadeIn(), slideDown(), and so on) in version 1.4.3 of jQuery, this was the defacto means of using easing with animation methods. Instead of providing string or numerical arguments (or a callback function), we can provide an object literal where each key refers to the duration, the easing type, and optionally a callback to call when the animation is complete. The usage then becomes as follows: jQuery(elements).slideDown({ duration: [duration], easing: [easing], complete: [callback] }); Time for action – using the alternative argument format This time we'll use the alternative syntax for supplying an easing function. 1. Change the call to the slideDown() method in slideEasing.html so that it appears as follows: $(this).addClass("on").next().slideDown({ easing: "easeOutBounce" }); 2. Save this version of the file as slideEasingObject.html. What just happened? By supplying an object literal as the first argument to the slideDown() method, we are able to make use of the easing types provided by the plugin in an alternative syntax. In this example, we omit the duration and complete keys of the object and supply only the name of the easing type as a string.

Sliding Animations Have a go hero – using easing Try out some of the other easing methods that are available via the easing plugin in this, and some of our earlier examples. We'll be using easing where appropriate throughout the remainder of the book, but other than a cursory explanation these won't be focused on in any great detail. Pop quiz – using easing 1. How many easing types does the easing plugin add? a. 20 b. 30 c. 17 d. 48 2. What can we pass into an effect method in the alternative format for using easing? a. An object with optional keys specifying the duration, easing type, and a function to call on complete b. A string specifying the easing type c. An array where the first item is the duration, the second is the easing type and the third is a function to call on complete d. An integer specifying the duration of easing The flicker effect Sometimes, using slideDown() and slideUp() animations on the same elements that are triggered when the visitor hovers over an element, such as with a navigation menu for example, can have an impact on the usability of a site. It's important to be aware of what the issue is and how it can be resolved. Time for action – avoiding the flicker effect Let's put together a couple of examples so that we can see which situations can cause the problem to arise. 1. Open up the fadeIn.html file from Chapter 2, and update the element so that it appears as follows (new code shown in bold): [ 92 ]

[ 91 ]<br />

Chapter 4<br />

Easing is a great effect that can be added with the addition <strong>of</strong> an 8 KB plugin (3.51 KB when<br />

minified and with the license moved to an external file) and a very minor tweak to our code.<br />

Using it is simple, but its effects can be enormous, transforming a monotonous or otherwise<br />

boring animation into one filled with impact and interest.<br />

Using an object literal to add easing<br />

We can also change the format <strong>of</strong> the arguments we pass into the predefined animation<br />

methods in order to use easing. Prior to the easing argument being added to the animation<br />

methods (fadeIn(), slideDown(), and so on) in version <strong>1.4</strong>.3 <strong>of</strong> <strong>jQuery</strong>, this was the defacto<br />

means <strong>of</strong> using easing with animation methods.<br />

Instead <strong>of</strong> providing string or numerical arguments (or a callback function), we can provide<br />

an object literal where each key refers to the duration, the easing type, and optionally a<br />

callback to call when the animation is complete. The usage then becomes as follows:<br />

<strong>jQuery</strong>(elements).slideDown({<br />

duration: [duration],<br />

easing: [easing],<br />

complete: [callback]<br />

});<br />

Time for action – using the alternative argument format<br />

This time we'll use the alternative syntax for supplying an easing function.<br />

1. Change the call to the slideDown() method in slideEasing.html so that it<br />

appears as follows:<br />

$(this).addClass("on").next().slideDown({<br />

easing: "easeOutBounce"<br />

});<br />

2. Save this version <strong>of</strong> the file as slideEasingObject.html.<br />

What just happened?<br />

By supplying an object literal as the first argument to the slideDown() method, we are<br />

able to make use <strong>of</strong> the easing types provided by the plugin in an alternative syntax. In this<br />

example, we omit the duration and complete keys <strong>of</strong> the object and supply only the<br />

name <strong>of</strong> the easing type as a string.

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

Saved successfully!

Ooh no, something went wrong!