jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
Delaying queue execution [ 69 ] Chapter 3 As of jQuery 1.4 we can choose to delay the execution of the next function in the queue using the delay() method. We need to supply the duration as an argument to the method to tell it how long the delay before the next effect starts should be, and we can optionally supply the name of the queue to delay as an argument as well. The method is used in this format: jQuery(elements).delay(duration, [queue name]); The duration argument may be supplied as an integer representing the length of the duration in milliseconds, just like the effect methods we've covered so far, or it may be one of the strings slow or fast which correspond to the standard values. If no duration is provided, the queue will not be delayed, and if a string other than slow or fast is provided, the delay will be the default duration of 400 milliseconds. The queue does not need to be directly manipulated in order to set a delay. All we need to do is chain the method between our animation methods, so an animation that fades an element in and out several times that required a delay could be constructed like this: $("#fader").fadeOut().delay(2000).fadeIn().fadeOut().fadeIn(); Note that the delay() method is only supposed to be used with methods or functions in a queue, just like the stop() method, and cannot (and is not meant to) replace JavaScript's setTimeout() function. Clearing the queue Plugins There are several great plugins that make working with both the setTimeout() and setInterval() native JavaScript functions quicker and easier. Just search the plugin repository for setTimeout. As well as viewing the queue and manipulating its contents, we can also remove all of the functions from it entirely. jQuery provides the clearQueue() method allowing us to easily clear all functions in the specified element's queue. Like dequeue(), this is a simple method that takes just a single optional argument: jQuery(elements).clearQueue([queue name]); This method is generally used with non-animation-based queues, when using the stop() method is not possible, and so will not be discussed further.
Managing Animations Passing an empty array to the queue() method will also clear the queue. Useful properties of the jQuery object The jQuery object contains a couple of properties that we can set which can be useful when creating animations. The jQuery (or $) object contains an fx property, which itself contains two properties related to animations which we can manipulate. This fx object is not to be confused with the fx queues that are created by default for any element that has more than one animation method called on it in a chain. These individual fx queues do not contain the same properties that the jQuery fx property contains. Globally disabling animations One property of fx that we can set is the off property. This property contains a Boolean that is set to false by default, but which we can set to true to globally disable all animations on a page. The property is set using the following syntax: jQuery.fx.off = true; That's all we do need to do. If this is set at any point in our script, all elements that have animation methods attached to them will be set to their final state, as if the animation had already completed. Changing the default frame rate The other property of jQuery's fx that we can set is the interval property. This property accepts an integer and specifies the number of milliseconds between each frame of the animation. By default, it is set to 13, so an animation will have a frame-rate of about 76 frames per second. To set this property, we just supply a different integer: jQuery.fx.interval = 28 Setting the property to 28 like this would make the animation run at about 35 frames per second, making animations run almost half as smoothly. Note that animations will still run over the same duration of time (whether that is the default 400 milliseconds, or another value set with the duration argument of an animation method) regardless of what this property is set to. However, an interval value that is lower, and therefore has a higher number of frames per second, will make animations appear smoother. [ 70 ]
- Page 40 and 41: 2 Fading Animations In this chapter
- Page 42 and 43: [ 21 ] Chapter 2 We can control the
- Page 44 and 45: The previous screenshot shows how t
- Page 46 and 47: [ 25 ] Chapter 2 Most of the styles
- Page 48 and 49: 2. What else can be passed into the
- Page 50 and 51: Time for action - creating the dial
- Page 52 and 53: [ 31 ] Chapter 2 For example, the a
- Page 54 and 55: Download from Wow! eBook The follo
- Page 56 and 57: [ 35 ] Chapter 2 The CSS is used mo
- Page 58 and 59: The fadeTo() method is used in the
- Page 60 and 61: [ 39 ] Chapter 2 #messageList table
- 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 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 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
Delaying queue execution<br />
[ 69 ]<br />
Chapter 3<br />
As <strong>of</strong> <strong>jQuery</strong> <strong>1.4</strong> we can choose to delay the execution <strong>of</strong> the next function in the queue<br />
using the delay() method. We need to supply the duration as an argument to the method<br />
to tell it how long the delay before the next effect starts should be, and we can optionally<br />
supply the name <strong>of</strong> the queue to delay as an argument as well. The method is used in<br />
this format:<br />
<strong>jQuery</strong>(elements).delay(duration, [queue name]);<br />
The duration argument may be supplied as an integer representing the length <strong>of</strong> the duration<br />
in milliseconds, just like the effect methods we've covered so far, or it may be one <strong>of</strong> the<br />
strings slow or fast which correspond to the standard values. If no duration is provided,<br />
the queue will not be delayed, and if a string other than slow or fast is provided, the delay<br />
will be the default duration <strong>of</strong> 400 milliseconds.<br />
The queue does not need to be directly manipulated in order to set a delay. All we need<br />
to do is chain the method between our animation methods, so an animation that fades an<br />
element in and out several times that required a delay could be constructed like this:<br />
$("#fader").fadeOut().delay(2000).fadeIn().fadeOut().fadeIn();<br />
Note that the delay() method is only supposed to be used with methods or functions in a<br />
queue, just like the stop() method, and cannot (and is not meant to) replace JavaScript's<br />
setTimeout() function.<br />
Clearing the queue<br />
Plugins<br />
There are several great plugins that make working with both the<br />
setTimeout() and setInterval() native JavaScript functions<br />
quicker and easier. Just search the plugin repository for setTimeout.<br />
As well as viewing the queue and manipulating its contents, we can also remove all <strong>of</strong> the<br />
functions from it entirely. <strong>jQuery</strong> provides the clearQueue() method allowing us to easily<br />
clear all functions in the specified element's queue.<br />
Like dequeue(), this is a simple method that takes just a single optional argument:<br />
<strong>jQuery</strong>(elements).clearQueue([queue name]);<br />
This method is generally used with non-animation-based queues, when using the stop()<br />
method is not possible, and so will not be discussed further.