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

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 ]

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.

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

Saved successfully!

Ooh no, something went wrong!