返回值:ArrayjQuery.queue(element, [queueName])
示例:
显示队列长度。
<!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; }
span { color:red; } </style>
<script src="jquery.min.js"></script>
</head>
<body>
<button id="show">Show Length of Queue</button>
<span></span>
<div></div>
<script>
$("#show").click(function () {
var n = jQuery.queue( $("div")[0], "fx" );
$("span").text("Queue length is: " + n.length);
});
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);
}
runIt();
</script>
</body>
</html>
演示:
返回值:jQueryjQuery.queue(element, queueName, newQueue)
操作匹配元素上将要执行的函数队列。
-
1.3 新增jQuery.queue(element, queueName, newQueue)
element
(Element)
带有函数队列数组的 DOM 元素。
queueName
(String)
包含队列名称的字符串,默认值是动画效果队列 fx
。
newQueue
(Array)
一个用于替换现有队列的函数数组。
-
1.3 新增jQuery.queue(element, queueName, callback())
element
(Element)
将要往队列中增加一个函数的 DOM 元素。
queueName
(String)
包含队列名称的字符串,默认值是动画效果队列 fx
。
callback()
(Function)
将要添加到队列中的新函数。
注意: 这是一个底层方法,应当使用更合适的 .queue()
来代替。
每个元素都可以通过jQuery附加一个或多个函数队列。在大多数程序中,只会使用一个队列(名为 fx
)。队列允许异步地对某个元素进行一系列操作,而不是把整个程序挂起。
可以通过 jQuery 为任何元素添加一个或多个函数队列。但在大多数应用中,通常只使用动画队列(fx
)。使用队列,我们可以在一个元素上以异步的方式执行一系列动作,而不需要暂停执行程序。
jQuery.queue()
允许我们直接操作这个函数队列。最常用的用法是调用 jQuery.queue()
时带一个回调函数,这样就能让我们在队列最后的添加一个新的函数。
注意,当通过 jQuery.queue()
增加一个函数时,务必确保在函数的最后调用了 jQuery.dequeue()
,以便能够执行队列中的下一个函数。
示例:
将一个自定义函数加入到队列中。
<!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="jquery.min.js"></script>
</head>
<body>
Click here...
<div></div>
<script>
$(document.body).click(function () {
$("div").show("slow");
$("div").animate({left:'+=200'},2000);
jQuery.queue( $("div")[0], "fx", function () {
$(this).addClass("newcolor");
jQuery.dequeue( this );
});
$("div").animate({left:'-=200'},500);
jQuery.queue( $("div")[0], "fx", function () {
$(this).removeClass("newcolor");
jQuery.dequeue( this );
});
$("div").slideUp();
});
</script>
</body>
</html>
演示:
示例:
通过设置一个队列数组,来删除已有的队列。
<!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="jquery.min.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);
jQuery.queue( $("div")[0], "fx", function () {
$(this).addClass("newcolor");
jQuery.dequeue( this );
});
$("div").animate({left:'-=200'},1500);
jQuery.queue( $("div")[0], "fx", function () {
$(this).removeClass("newcolor");
jQuery.dequeue( this );
});
$("div").slideUp();
});
$("#stop").click(function () {
jQuery.queue( $("div")[0], "fx", [] );
$("div").stop();
});
</script>
</body>
</html>
演示: