.queue()


显示或操作匹配的元素上已经执行的函数列队。

Contents:

.queue( [queueName ] )返回: Array

描述: 显示在匹配的元素上的已经执行的函数列队。

  • 添加的版本: 1.2.queue( [queueName ] )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。

例子:

显示列队的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
<style>div { margin:3px; width:40px; height:40px;
position:absolute; left:0px; top:60px;
background:green; display:none; }
div.newcolor { background:blue; }
p { color:red; } </style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>The queue length is: <span></span></p>
<div></div>
<script>
var div = $("div");
function runIt() {
div.show("slow");
div.animate({left:'+=200'},2000);
div.slideToggle(1000);
div.slideToggle("fast");
div.animate({left:'-=200'},1500);
div.hide("slow");
div.show(1200);
div.slideUp("normal", runIt);
}
function showIt() {
var n = div.queue("fx");
$("span").text( n.length );
setTimeout(showIt, 100);
}
runIt();
showIt();
</script>
</body>
</html>

Demo:

.queue( [queueName ], newQueue )返回: jQuery

描述: 在匹配元素上操作已经附加函数的列表。

  • 添加的版本: 1.2.queue( [queueName ], newQueue )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。
    • newQueue
      类型: Array
      一个替换当前列队内容的函数数组。
  • 添加的版本: 1.2.queue( [queueName ], callback( next ) )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。
    • callback( next )
      类型: Function()
      将要添加到队列中的新函数。当该函数被调用时,会从弹出队列中的下一个元素。

每个元素可以通过jQuery,包含一个或多个函数队列。在大多数应用中,只有一个列队(访问 fx)被使用。队列允许一个元素来异步的访问一连串的动作,而不终止程序执行。典型的例子就是在一个元素上调用多重动画的方法对一个元素。例如:

1
$('#foo').slideUp().fadeIn();

当这个语句被执行,这个元素开始立即做滑动动画,但渐入动画放置在 fx 列队在,只有当滑动动画完成后才会被执行。

queue()方法允许我们直接操纵这个函数队列。用一个回调函数访问queue()特别的有用;它让我们把新函数置入到队列的末端。为jQuery集合中的每个元素执行一次回调函数。

该函数的功能类似于在动画方法中提供了回调函数,但是不要求在动画执行时指定回调函数。

1
2
3
4
5
$('#foo').slideUp();
$('#foo').queue(function() {
alert('Animation complete.');
$(this).dequeue();
});

这是相当于:

1
2
3
$('#foo').slideUp(function() {
alert('Animation complete.');
});

值得注意的是,当使用.queue()添加一个函数的时候,我们应该保证在函数最后调用了 jQuery.dequeue(),这样就能让队列中的其它函数按顺序执行。

从jQuery 1.4开始,向队列中追加函数时,可以向该函数中传入另一个函数,作为第一个参数。当调用函数时,会自动从函数队列中弹出下一个项目,保证队列中函数的继续进行。我们可以像下面这样使用:

1
2
3
4
$("#test").queue(function(next) {
// Do some stuff...
next();
});

例子:

Example: Queue a custom function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html>
<head>
<style>
div { margin:3px; width:40px; height:40px;
position:absolute; left:0px; top:30px;
background:green; display:none; }
div.newcolor { background:blue; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
Click here...
<div></div>
<script>$(document.body).click(function () {
$("div").show("slow");
$("div").animate({left:'+=200'},2000);
$("div").queue(function () {
$(this).addClass("newcolor");
$(this).dequeue();
});
$("div").animate({left:'-=200'},500);
$("div").queue(function () {
$(this).removeClass("newcolor");
$(this).dequeue();
});
$("div").slideUp();
});</script>
</body>
</html>

Demo:

Example: Set a queue array to delete the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<head>
<style>
div { margin:3px; width:40px; height:40px;
position:absolute; left:0px; top:30px;
background:green; display:none; }
div.newcolor { background:blue; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<div></div>
<script>$("#start").click(function () {
$("div").show("slow");
$("div").animate({left:'+=200'},5000);
$("div").queue(function () {
$(this).addClass("newcolor");
$(this).dequeue();
});
$("div").animate({left:'-=200'},1500);
$("div").queue(function () {
$(this).removeClass("newcolor");
$(this).dequeue();
});
$("div").slideUp();
});
$("#stop").click(function () {
$("div").queue("fx", []);
$("div").stop();
});</script>
</body>
</html>

Demo: