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

[ 57 ] Chapter 3 A custom queue that we create ourselves will not run automatically and we must ensure that each item in the queue is executed one after the other. jQuery provides several ways of doing this including the dequeue() method, which executes the next function in the queue, and a callback function that we can pass into functions in the queue. We'll look at both of these techniques later in the chapter. Viewing the queue To view the queue we simply call the queue() method; no arguments are required but we can optionally supply the queue name if it differs from the default fx. When the method is called, it returns an array containing the remaining functions in the queue. The queue() method may be used in the following form: jQuery(elements).queue([queue name], [new queue], [callback]); In addition to the name of the queue, we can also supply either a new queue, or a single callback function. The new queue, if supplied, should take the form of an array of functions. The new queue will replace the existing queue entirely. Passing an empty array to the queue() method will clear the queue. A callback passed to the queue() method will be added to the end of the queue and executed last after the functions originally in the queue. A common use of the queue() method is to look at the length property of the returned array to determine how many functions are left to run; but if we need to, we can also look at each function individually by calling the toString() JavaScript function on any item in the array (except for item 0 which will simply display the string inprogress). Most functions in the returned array are function literals, however, the "next" item in the queue is not available via the queue() method. The contents of item 0 in the default fx queue will always be the string inprogress as this is the animation currently being run. Time for action - viewing the queue Let's look at a basic example of the use of the queue() method and the type of results we can expect to obtain. 1. In the of our template file add the following code: #fader { width:100px; height:100px; background-color:#000; }

Managing Animations 2. Finally, in the anonymous function at the bottom of the second element, add the following code: $("#fader").fadeOut(function() { console.log($(this).queue()); }).fadeIn().fadeOut().fadeIn(); 3. Save the page as queue.html. What just happened? Typically, we'd use an external stylesheet for any styling, but for a single selector and three rules, it seems pointless creating a separate file. In the script we have four effects chained together to form a simple animation in which our element is simply faded out and back in twice. We provide a callback function as an argument to the first of our effect methods, within which we call the queue() method. You'll need to use a browser that has a console for this example, such as Firefox. Here's what the output looks like in Firebug: In the previous screenshot, we can see that the array making up the queue has been output to the console for us to view. There are three items left in the queue when the method is called. The first item in the array is the string inprogress. The remaining two items are the queued methods that have not yet run. [ 58 ]

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

2. Finally, in the anonymous function at the bottom <strong>of</strong> the second element,<br />

add the following code:<br />

$("#fader").fadeOut(function() {<br />

console.log($(this).queue());<br />

}).fadeIn().fadeOut().fadeIn();<br />

3. Save the page as queue.html.<br />

What just happened?<br />

Typically, we'd use an external stylesheet for any styling, but for a single selector and three<br />

rules, it seems pointless creating a separate file. In the script we have four effects chained<br />

together to form a simple animation in which our element is simply faded out and<br />

back in twice. We provide a callback function as an argument to the first <strong>of</strong> our effect<br />

methods, within which we call the queue() method.<br />

You'll need to use a browser that has a console for this example, such as Firefox. Here's what<br />

the output looks like in Firebug:<br />

In the previous screenshot, we can see that the array making up the queue has been output<br />

to the console for us to view. There are three items left in the queue when the method is<br />

called. The first item in the array is the string inprogress. The remaining two items are the<br />

queued methods that have not yet run.<br />

[ 58 ]

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

Saved successfully!

Ooh no, something went wrong!