|
4 | 4 | "source": "http://jqueryfordesigners.com/api-queue-dequeue/"
|
5 | 5 | }</script>
|
6 | 6 |
|
7 |
| -When you use the `.animate()`, `.show()`, `.hide()`, `.slideUp()`, etc. effect methods, you're adding a job to the effects queue. By default, using `.queue()` and passing a function, will add it to the effects queue. So we're creating our own bespoke animation step: |
| 7 | +Queues are the foundation for all animations in jQuery, they allow a series functions to be executed asynchronously on an element. Methods such as `.slideUp()`, `.slideDown()`, `.fadeIn()`, and `.fadeOut()` all use `.animate()`, which leverages *queues* to build up the series of steps that will transition one or more CSS values throughout the duration of the animation. |
| 8 | + |
| 9 | +We can pass a callback function to the `.animate()` method, which will execute once the animation has completed. |
8 | 10 |
|
9 | 11 | ```
|
10 | 12 | $( ".box" )
|
11 |
| - .animate({ |
| 13 | + .animate( { |
12 | 14 | height: 20
|
13 |
| - }, "slow" ) |
14 |
| - .queue(function() { |
15 |
| - $( "#title" ).html( "We're in the animation, baby!" ); |
16 |
| - }); |
| 15 | + }, "slow", function() { |
| 16 | + $( "#title" ).html( "We're in the callback, baby!" ); |
| 17 | + } ); |
17 | 18 | ```
|
18 | 19 |
|
19 |
| -As I said though, these methods come in pairs, so anything you add using `.queue()`, you need to dequeue to allow the process to continue. In the code above, if I chained more animations on, until I call `$( this ).dequeue()`, the subsequent animations wouldn't run: |
| 20 | +## Queues As Callbacks |
| 21 | + |
| 22 | +Instead of passing a callback as an argument, we can add another function to the *queue* that will act as our callback. This will execute after all of the steps in the animation have completed. |
20 | 23 |
|
21 | 24 | ```
|
22 | 25 | $( ".box" )
|
23 |
| - .animate({ |
| 26 | + .animate( { |
24 | 27 | height: 20
|
25 |
| - }, "slow" ) |
26 |
| - .queue(function() { |
| 28 | + }, "slow") |
| 29 | + .queue( function() { |
27 | 30 | $( "#title" ).html( "We're in the animation, baby!" );
|
| 31 | +
|
| 32 | + // This tells jQuery to continue to the next item in the queue |
28 | 33 | $( this ).dequeue();
|
29 |
| - }).animate({ |
30 |
| - height: 150 |
31 |
| - }); |
| 34 | + } ); |
| 35 | +
|
32 | 36 | ```
|
33 | 37 |
|
34 |
| -Keeping in mind that the animation won't continue until we've explicitly called `.dequeue()`, we can easily create a pausing plugin, by adding a step in the queue that sets a timer and triggers after `delay` milliseconds, at which time, it dequeues the element: |
| 38 | +In this example, the queued function will execute right after the animation. |
| 39 | + |
| 40 | +jQuery does not have any insight into how the queue items function, so we need to call `.dequeue()`, which tells jQuery when to move to the next item in the queue. |
35 | 41 |
|
| 42 | +Another way of *dequeuing* is by calling the function that is passed to your callback. That function will automatically call `.dequeue()` for you. |
| 43 | + |
| 44 | +``` |
| 45 | +.queue( function( next ) { |
| 46 | + console.log( "I fired!" ); |
| 47 | + next(); |
| 48 | +} ); |
36 | 49 | ```
|
37 |
| -$.fn.pause = function( delay ) { |
38 |
| - return this.queue(function() { |
39 |
| - var elem = this; |
40 |
| - setTimeout(function() { |
41 |
| - return $( elem ).dequeue(); |
42 |
| - }, delay ); |
43 |
| - }); |
44 |
| -}; |
45 | 50 |
|
| 51 | +## Custom Queues |
| 52 | + |
| 53 | +Up to this point all of our animation and queue examples have been using the default queue name which is `fx`. Elements can have multiple queues attached to them, and we can give each of these queues a different name. We can specify a custom queue name as the first argument to the `.queue()` method. |
| 54 | + |
| 55 | +``` |
46 | 56 | $( ".box" )
|
47 |
| - .animate({ |
48 |
| - height: 20 |
49 |
| - }, "slow" ) |
50 |
| - .pause( 1000 ) |
51 |
| - .animate({ |
52 |
| - height: 150 |
53 |
| - }); |
| 57 | + .queue( "steps", function( next ) { |
| 58 | + console.log( "Step 1" ); |
| 59 | + next(); |
| 60 | + } ) |
| 61 | + .queue( "steps", function( next ) { |
| 62 | + console.log( "Step 2" ); |
| 63 | + next(); |
| 64 | + } ) |
| 65 | + .dequeue( "steps" ); |
| 66 | +``` |
| 67 | + |
| 68 | +Notice that we have to call the `.dequeue()` method passing it the name of our custom queue to start the execution. Every queue except for the default, `fx`, has to be manually kicked off by calling `.dequeue()` and passing it the name of the queue. |
| 69 | + |
| 70 | +## Clearing The Queue |
| 71 | + |
| 72 | +Since queues are just a set of ordered operations, our application may have some logic in place that needs to prevent the remaining queue entries from executing. We can do this by calling the `.clearQueue()` method, which will empty the queue. |
| 73 | + |
| 74 | +``` |
| 75 | +$( ".box" ) |
| 76 | + .queue( "steps", function( next ) { |
| 77 | + console.log( "Will never log because we clear the queue" ); |
| 78 | + next(); |
| 79 | + } ) |
| 80 | + .clearQueue( "steps" ) |
| 81 | + .dequeue( "steps" ); |
| 82 | +``` |
| 83 | + |
| 84 | +In this example, nothing will happen as we removed everything from the `steps` queue. |
| 85 | + |
| 86 | +Another way of clearing the queue is to call `.stop( true )`. That will stop the currently running animations and will clear the queue. |
| 87 | + |
| 88 | +## Replacing The Queue |
| 89 | + |
| 90 | +When you pass an array of functions as second argument to `.queue()`, that array will replace the queue. |
| 91 | + |
| 92 | +``` |
| 93 | +$( ".box" ) |
| 94 | + .queue( "steps", function( next ) { |
| 95 | + console.log( "I will never fire as we totally replace the queue" ); |
| 96 | + next(); |
| 97 | + } ) |
| 98 | + .queue( "steps", [ |
| 99 | + function( next ) { |
| 100 | + console.log( "I fired!" ); |
| 101 | + next(); |
| 102 | + } |
| 103 | + ] ) |
| 104 | + .dequeue( "steps" ); |
| 105 | +``` |
| 106 | + |
| 107 | +You can also call `.queue()` without passing it functions, which will return the queue of that element as an array. |
| 108 | + |
54 | 109 | ```
|
| 110 | +$( ".box" ).queue( "steps", function( next ) { |
| 111 | + console.log( "I fired!" ); |
| 112 | + next(); |
| 113 | +} ); |
55 | 114 |
|
56 |
| -Remember that the first argument for `.queue()` and `.dequeue()` is `fx`, and that in all of these examples I'm not including it because jQuery sets the argument to `fx` by default — so I don't have to specify it. |
| 115 | +console.log( $( ".box" ).queue( "steps" ) ); |
| 116 | +
|
| 117 | +$( ".box" ).dequeue( "steps" ); |
| 118 | +``` |
0 commit comments