.queue()
.queue( [ queueName ] ) 返回: Array
描述: 顯示在匹配的元素上的已經(jīng)執(zhí)行的函數(shù)列隊(duì)。
-
version added: 1.2.queue( [ queueName ] )
queueName一個(gè)含有隊(duì)列名的字符串。默認(rèn)是"Fx",標(biāo)準(zhǔn)的動(dòng)畫隊(duì)列。
Example:
顯示列隊(duì)的長(zhǎng)度。
<!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.min.js"></script>
</head>
<body>
<button id="show">Show Length of Queue</button>
<p></p>
<div></div>
<script>$("#show").click(function () {
var n = $("div").queue("fx");
$("p").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>
Demo:
.queue( [ queueName ], newQueue ) 返回: jQuery
描述: 在匹配元素上操作已經(jīng)附加函數(shù)的列表。
-
version added: 1.2.queue( [ queueName ], newQueue )
queueName一個(gè)含有隊(duì)列名的字符串。默認(rèn)是"Fx",標(biāo)準(zhǔn)的動(dòng)畫隊(duì)列。
newQueue一個(gè)替換當(dāng)前函數(shù)列隊(duì)內(nèi)容的數(shù)組。
-
version added: 1.2.queue( [ queueName ], callback( next ) )
queueName一個(gè)含有隊(duì)列名的字符串。默認(rèn)是
fx,標(biāo)準(zhǔn)的動(dòng)畫隊(duì)列。callback( next )添加到列隊(duì)的新函數(shù)。
每個(gè)元素可以通過jQuery包含一個(gè)或多個(gè)函數(shù)隊(duì)列。在大多數(shù)應(yīng)用中,只有一個(gè)列隊(duì)(訪問 fx)被使用。隊(duì)列允許一個(gè)元素來(lái)異步的訪問一連串的動(dòng)作,而不終止程序執(zhí)行。典型的例子就是在一個(gè)元素上調(diào)用多重動(dòng)畫的方法對(duì)一個(gè)元素。例如:
$('#foo').slideUp().fadeIn();
當(dāng)這個(gè)語(yǔ)句被執(zhí)行,這個(gè)元素開始立即做滑動(dòng)動(dòng)畫,但漸入動(dòng)畫放置在 fx 列隊(duì)在,只有當(dāng)滑動(dòng)動(dòng)畫完成后才會(huì)被執(zhí)行。
queue()方法允許我們直接操縱這個(gè)函數(shù)隊(duì)列。用一個(gè)回調(diào)函數(shù)訪問queue()特別的有用;它讓我們把新函數(shù)置入到隊(duì)列的末端。
此特征是提供一個(gè)回調(diào)函數(shù)和動(dòng)畫的方法類似 ,但并不需要回調(diào)函數(shù)給定當(dāng)時(shí)的執(zhí)行動(dòng)畫。
$('#foo').slideUp();
$('#foo').queue(function() {
alert('Animation complete.');
$(this).dequeue();
});
This is equivalent to:
$('#foo').slideUp(function() {
alert('Animation complete.');
});
值得注意的是,當(dāng)使用queue()添加一個(gè)函數(shù)的時(shí)候,我們必須保證jQuery.dequeue()讓下一個(gè)函數(shù)執(zhí)行后被呼叫 。
在jQuery 1.4中這個(gè)函數(shù)通過另一個(gè)函數(shù)調(diào)用的。作為第一個(gè)參數(shù),他將調(diào)用自動(dòng)執(zhí)行下一項(xiàng)而且保持列隊(duì)移動(dòng)。你可以這樣使用:
$("#test").queue(function(next) {
// Do some stuff...
next();
});
例子:
舉例: Queue a custom function.
<!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.min.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.
<!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.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);
$("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>