jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 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 ]
- Page 62 and 63: [ 41 ] Chapter 2 What just happened
- Page 64 and 65: 2. What format can these arguments
- Page 66 and 67: } var el = $(this); if (el.css("fil
- Page 68 and 69: [ 47 ] Chapter 2 Usually these meth
- Page 70 and 71: Time for action - animations with s
- Page 72 and 73: Download from Wow! eBook [ 51 ] Ch
- Page 74 and 75: 2. Save the changes as toggle.html.
- Page 76 and 77: 3 Managing Animations Like most of
- Page 78 and 79: [ 57 ] Chapter 3 A custom queue tha
- Page 80 and 81: If we wish to see how many function
- Page 82 and 83: 2. What does the queue() method ret
- Page 84 and 85: [ 63 ] Chapter 3 The argument we su
- Page 86 and 87: [ 65 ] Chapter 3 We pass a callback
- Page 88 and 89: [ 67 ] Chapter 3 The first argument
- Page 90 and 91: Delaying queue execution [ 69 ] Cha
- Page 92 and 93: [ 71 ] Chapter 3 Also note that the
- Page 94 and 95: 4 Sliding Animations Another type o
- Page 96 and 97: Time for action - creating a slide-
- Page 98 and 99: [ 77 ] Chapter 4 header form input
- Page 100 and 101: Pop quiz - sliding elements down 1.
- Page 102 and 103: Download from Wow! eBook }); }); $
- Page 104 and 105: Toggling the slide [ 83 ] Chapter 4
- Page 106 and 107: [ 85 ] Chapter 4 } h2 { margin:0; p
- Page 108 and 109: [ 87 ] Chapter 4 The previous scree
- Page 110 and 111: Pop quiz - using slideToggle 1. Wha
- Page 114 and 115: [ 93 ] Chapter 4 Home Articles J
- Page 116 and 117: [ 95 ] Chapter 4 3. Save this file
- Page 118 and 119: In the previous screenshot, we see
- Page 120 and 121: Pop quiz - fixing the flicker 1. Wh
- Page 122 and 123: 5 Custom Animations The predefined
- Page 124 and 125: An alternative syntax for animate()
- Page 126 and 127: [ 105 ] Chapter 5 width:295px; marg
- Page 128 and 129: [ 107 ] Chapter 5 What just happene
- Page 130 and 131: [ 109 ] Chapter 5 Although not esse
- Page 132 and 133: [ 111 ] Chapter 5 The previous scre
- Page 134 and 135: [ 113 ] Chapter 5 The styles we use
- Page 136 and 137: [ 115 ] Chapter 5 What just happene
- Page 138 and 139: }, html: copy, width: expander.widt
- Page 140 and 141: [ 119 ] Chapter 5 If the image alre
- Page 142 and 143: [ 121 ] Chapter 5 d. Two objects wh
- Page 144 and 145: [ 123 ] Chapter 5 text-align:center
- Page 146 and 147: [ 125 ] Chapter 5 What just happene
- Page 148 and 149: [ 127 ] Chapter 5 Finally, we retur
- Page 150 and 151: [ 129 ] Chapter 5 Once we've define
- Page 152 and 153: } }; $(config.selector).find("a").c
- Page 154 and 155: [ 133 ] Chapter 5 In the previous s
- Page 156: Have a go hero - extending the plug
- Page 159 and 160: Extended Animations with jQuery UI
- Page 161 and 162: Extended Animations with jQuery UI
[ 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.