jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
5 Custom Animations The predefined effects that we have looked at throughout the book so far are very good at what they do, but they are there to cater for very specific requirements and will sometimes not be enough when more complex animations are needed. In these situations we can use jQuery's animate() method, which allows us to define custom animations with ease that can be as complex and as specialized as the task at hand requires, and this is what we'll be looking at over the course of this chapter. Subjects that we'll cover throughout the course of this chapter will include: Creating custom animations with the animate() method Passing arguments to the method Animating an element's dimensions Animating an element's position Creating a jQuery animation plugin
Custom Animations The animate method All custom animations with jQuery are driven with the animate() method. Despite the ability to animate almost any style property that has a numeric value, the method is simple to use and takes just a few arguments. The method may be used in the following way: jQuery(elements).animate(properties to animate, [duration], [easing], [callback] ); The first argument should take the form of an object where each property of the object is a style that we'd like to animate, very similar to how we would use jQuery's css() method. As I mentioned before, this can be any CSS style that takes a purely numerical argument (with the exception of colors, although with the jQuery UI library we can animate colors as well. See Chapter 6, Extended Animations with jQuery UI for more information on jQuery UI). Background positions cannot be animated by jQuery natively, but it is quite easy to animate this property manually; see Chapter 7, Full Page Animations for more information on this technique. The duration, easing, and callback arguments take the same formats as those that we used with the fading and sliding methods earlier in the book and are used in exactly the same way. Per-property easing As of the 1.4 version of jQuery, we can apply different types of easing to each style property we are animating when using the animate() method. So if we are animating both the width and height of an element for example, we can use linear easing for the width animation, and swing easing for the height animation. This applies to the standard easing functions built into jQuery, or any of the easing functions added with the easing plugin that we looked at in Chapter 4, Sliding Animations. To supply easing types to the animate() method on a per-property basis, we need to provide an array as the value of the property we are animating. This can be done using the following syntax: jQuery(elements).animate({ property: [value, easingType] }); [ 102 ]
- 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 112 and 113: [ 91 ] Chapter 4 Easing is a great
- 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 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
- Page 163 and 164: Extended Animations with jQuery UI
- Page 165 and 166: Extended Animations with jQuery UI
- Page 167 and 168: Extended Animations with jQuery UI
- Page 169 and 170: Extended Animations with jQuery UI
- Page 171 and 172: Download from Wow! eBook Extended
Custom <strong>Animation</strong>s<br />
The animate method<br />
All custom animations with <strong>jQuery</strong> are driven with the animate() method. Despite the<br />
ability to animate almost any style property that has a numeric value, the method is simple<br />
to use and takes just a few arguments. The method may be used in the following way:<br />
<strong>jQuery</strong>(elements).animate(properties to animate,<br />
[duration],<br />
[easing],<br />
[callback]<br />
);<br />
The first argument should take the form <strong>of</strong> an object where each property <strong>of</strong> the object is a<br />
style that we'd like to animate, very similar to how we would use <strong>jQuery</strong>'s css() method.<br />
As I mentioned before, this can be any CSS style that takes a purely numerical argument<br />
(with the exception <strong>of</strong> colors, although with the <strong>jQuery</strong> UI library we can animate colors as<br />
well. See Chapter 6, Extended <strong>Animation</strong>s with <strong>jQuery</strong> UI for more information on <strong>jQuery</strong> UI).<br />
Background positions cannot be animated by <strong>jQuery</strong> natively, but it is quite easy to animate<br />
this property manually; see Chapter 7, Full Page <strong>Animation</strong>s for more information on<br />
this technique.<br />
The duration, easing, and callback arguments take the same formats as those that we used<br />
with the fading and sliding methods earlier in the book and are used in exactly the same way.<br />
Per-property easing<br />
As <strong>of</strong> the <strong>1.4</strong> version <strong>of</strong> <strong>jQuery</strong>, we can apply different types <strong>of</strong> easing to each style property<br />
we are animating when using the animate() method. So if we are animating both the<br />
width and height <strong>of</strong> an element for example, we can use linear easing for the width<br />
animation, and swing easing for the height animation. This applies to the standard easing<br />
functions built into <strong>jQuery</strong>, or any <strong>of</strong> the easing functions added with the easing plugin that<br />
we looked at in Chapter 4, Sliding <strong>Animation</strong>s.<br />
To supply easing types to the animate() method on a per-property basis, we need to<br />
provide an array as the value <strong>of</strong> the property we are animating. This can be done using the<br />
following syntax:<br />
<strong>jQuery</strong>(elements).animate({<br />
property: [value, easingType]<br />
});<br />
[ 102 ]