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

3 Managing Animations Like most of the methods that jQuery makes available to us, the effect, or animation, methods can be chained together in sequence, like this: jQuery(elements).fadeIn().slideDown() When several animation methods, such as fadeIn(), and so on, are chained together and called on the same element or collection of elements, they are placed into a queue to be executed one after the other in series rather than all executing simultaneously in parallel. The standard animation queue created for an animated element is called fx, although custom queues can easily be created. jQuery gives us several methods that make working with and manipulating an element's fx queue extremely easy. These methods include: clearQueue() delay() dequeue() queue() stop() The techniques that we'll learn in this chapter include: Viewing the items in an element's queue Counting the items in an element's queue Preventing queued effects from executing Delaying the start of the next effect in the queue

Managing Animations Replacing the existing queue with a new queue Calling the next function in a custom queue Stopping the current effect without executing the rest of the queue Globally disabling all animations Changing the frame rate of animations globally The jQuery object itself also contains several properties that can be useful when working with animations. These include: jQuery.fx.off jQuery.fx.interval The queue is not restricted to storing animation methods; other methods can also be added to the queue. We will only be looking at the queue() method from an animation perspective here. Working with the queue When several effects are chained together, the first effect is begun straight away. The remaining effects are stored as functions in an array in the element's fx queue. As the effects are stored in an array, we can call standard JavaScript array methods on it to manipulate it, and examine its properties to find out additional information about it. We can determine how many functions are in the queue by looking at the length property of the array, or we can call standard functions such as push(), pop(), or reverse() on it to perform various operations on the items (functions) in the array. It is unlikely that this would be required in most normal situations however. An important point to note about the queue is that the first effect method called on an element does not get stored in the queue, so the length of the queue at the start of the operation will always be one less than the total number of effect methods called on the element. The queue executes on a first-in-first-out basis, with the last function stored in the queue executing last. The default fx queue for an animated element will run automatically and each function contained within it will be called automatically by jQuery. The string inprogress is used as the first item in the default fx queue as a flag indicating that the queue is currently being run. [ 56 ]

Managing <strong>Animation</strong>s<br />

Replacing the existing queue with a new queue<br />

Calling the next function in a custom queue<br />

Stopping the current effect without executing the rest <strong>of</strong> the queue<br />

Globally disabling all animations<br />

Changing the frame rate <strong>of</strong> animations globally<br />

The <strong>jQuery</strong> object itself also contains several properties that can be useful when working<br />

with animations. These include:<br />

<strong>jQuery</strong>.fx.<strong>of</strong>f<br />

<strong>jQuery</strong>.fx.interval<br />

The queue is not restricted to storing animation methods; other methods can<br />

also be added to the queue. We will only be looking at the queue() method<br />

from an animation perspective here.<br />

Working with the queue<br />

When several effects are chained together, the first effect is begun straight away. The<br />

remaining effects are stored as functions in an array in the element's fx queue. As the<br />

effects are stored in an array, we can call standard JavaScript array methods on it to<br />

manipulate it, and examine its properties to find out additional information about it.<br />

We can determine how many functions are in the queue by looking at the length property<br />

<strong>of</strong> the array, or we can call standard functions such as push(), pop(), or reverse() on<br />

it to perform various operations on the items (functions) in the array. It is unlikely that this<br />

would be required in most normal situations however.<br />

An important point to note about the queue is that the first effect method called on an<br />

element does not get stored in the queue, so the length <strong>of</strong> the queue at the start <strong>of</strong><br />

the operation will always be one less than the total number <strong>of</strong> effect methods called on<br />

the element.<br />

The queue executes on a first-in-first-out basis, with the last function stored in the queue<br />

executing last. The default fx queue for an animated element will run automatically<br />

and each function contained within it will be called automatically by <strong>jQuery</strong>. The string<br />

inprogress is used as the first item in the default fx queue as a flag indicating that the<br />

queue is currently being run.<br />

[ 56 ]

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

Saved successfully!

Ooh no, something went wrong!