Skip to content

Commit 0f9e7e0

Browse files
committed
adding queue/dequeue post
1 parent 5e5df58 commit 0f9e7e0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Queue & Dequeue Explained
3+
attribution: Remy Sharp
4+
source: http://jqueryfordesigners.com/api-queue-dequeue/
5+
---
6+
7+
When you use the animate and show, hide, slideUp, etc effect methods, you’re adding a job on to the fx queue.By default, using queue and passing a function, will add to the fx queue. So we’re creating our own bespoke animation step:
8+
9+
<div class="example" markdown="1">
10+
$('.box').animate({
11+
height : 20
12+
}, 'slow')
13+
.queue(function () {
14+
$('#title').html("We're in the animation, baby!");
15+
});
16+
</div>
17+
18+
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:
19+
20+
<div class="example" markdown="1">
21+
$('.box').animate({
22+
height : 20
23+
}, 'slow')
24+
.queue(function () {
25+
$('#title').html("We're in the animation, baby!");
26+
$(this).dequeue();
27+
})
28+
.animate({
29+
height: 150
30+
});
31+
</div>
32+
33+
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 n milliseconds, at which time, it dequeues the element:
34+
35+
<div class="example" markdown="1">
36+
$.fn.pause = function (n) {
37+
return this.queue(function () {
38+
var el = this;
39+
setTimeout(function () {
40+
return $(el).dequeue();
41+
}, n);
42+
});
43+
};
44+
45+
$('.box').animate({
46+
height : 20
47+
}, 'slow')
48+
.pause(1000) // 1000ms == 1 second
49+
.animate({
50+
height: 150
51+
});
52+
</div>
53+
54+
Remember that the first argument for queue and dequeue are ‘fx’, and that in all of these examples I’m not including it because jQuery set the argument to ‘fx’ by default - so I don’t have to specify it.

0 commit comments

Comments
 (0)