02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ 57 ]<br />

Chapter 3<br />

A custom queue that we create ourselves will not run automatically and we must ensure<br />

that each item in the queue is executed one after the other. <strong>jQuery</strong> provides several ways <strong>of</strong><br />

doing this including the dequeue() method, which executes the next function in the queue,<br />

and a callback function that we can pass into functions in the queue. We'll look at both <strong>of</strong><br />

these techniques later in the chapter.<br />

Viewing the queue<br />

To view the queue we simply call the queue() method; no arguments are required but we<br />

can optionally supply the queue name if it differs from the default fx. When the method<br />

is called, it returns an array containing the remaining functions in the queue. The queue()<br />

method may be used in the following form:<br />

<strong>jQuery</strong>(elements).queue([queue name], [new queue], [callback]);<br />

In addition to the name <strong>of</strong> the queue, we can also supply either a new queue, or a single<br />

callback function. The new queue, if supplied, should take the form <strong>of</strong> an array <strong>of</strong> functions.<br />

The new queue will replace the existing queue entirely. Passing an empty array to the<br />

queue() method will clear the queue. A callback passed to the queue() method will be<br />

added to the end <strong>of</strong> the queue and executed last after the functions originally in the queue.<br />

A common use <strong>of</strong> the queue() method is to look at the length property <strong>of</strong> the returned<br />

array to determine how many functions are left to run; but if we need to, we can also look at<br />

each function individually by calling the toString() JavaScript function on any item in the<br />

array (except for item 0 which will simply display the string inprogress).<br />

Most functions in the returned array are function literals, however, the "next" item in the<br />

queue is not available via the queue() method. The contents <strong>of</strong> item 0 in the default fx<br />

queue will always be the string inprogress as this is the animation currently being run.<br />

Time for action - viewing the queue<br />

Let's look at a basic example <strong>of</strong> the use <strong>of</strong> the queue() method and the type <strong>of</strong> results we<br />

can expect to obtain.<br />

1. In the <strong>of</strong> our template file add the following code:<br />

<br />

#fader { width:100px; height:100px; background-color:#000; }<br />

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

Saved successfully!

Ooh no, something went wrong!