jQuery中queue()方法用法實例
本文實例講述了jQuery中queue()方法用法。分享給大家供大家參考。具體分析如下:
此方法能夠顯示或者操作在匹配元素上執(zhí)行的函數(shù)隊列。
此方法可能用的并不是太頻繁,但是卻非常的重要,下面就結(jié)合實例來介紹一下次方法的用法。
根據(jù)方法參數(shù)的不同,作用也有所不同。
說明:建議結(jié)合dequeue()函數(shù)一起學習。
語法結(jié)構(gòu)一:
參數(shù)列表:
沒有參數(shù)的時候,能夠返回第一個匹配元素上的動畫隊列。
實例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>queue()函數(shù)-腳本之家</title>
<style type="text/css">
.box{
width:300px;
height:150px;
}
.mytest{
width:50px;
height:50px;
background-color:green;
position:absolute;
left:0px;
display:none;
font-size:12px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#do").click(function(){
$(".mytest").show(1000);
$(".mytest").animate({left:"+=200"},3000);
$(".mytest").animate({top:"+=50"},2000);
$(".mytest").text("動畫完成");
})
$("#count").click(function(){
alert($(".mytest").queue().length)
})
})
</script>
</head>
<body>
<div class="box">
<div class="mytest"></div>
</div>
<button id="do">點擊開始動畫</button>
<button id="count">計算隊列中函數(shù)數(shù)量</button>
</body>
</html>
由于queue()函數(shù)沒有參數(shù),所以返回值是第一個匹配元素上的動畫隊列,也就是div元素的動畫隊列,當點擊第二個按鈕的時候能夠?qū)崟r的計算出當前隊列中的動畫個數(shù)。
語法二:
可以為匹配元素的函數(shù)隊列最后面添加一個函數(shù)。
參數(shù)列表:
在語法一的實例中,大家可能注意到一個問題,那就是我們希望在所有的動畫都完成之后,再在div中添加“動畫完成”四個字,但是從運行的實際表現(xiàn)來看,并非如此,這主要的原因是,show()和animate()動畫函數(shù)會默認的添加到fx動畫隊列中,而text()方法并非動畫函數(shù),所以不會加入到fx隊列,并且會首先執(zhí)行。那么可以通過使用此函數(shù),將text()方法入隊。
實例代碼:
實例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>queue()函數(shù)-腳本之家</title>
<style type="text/css">
.box{
width:300px;
height:150px;
}
.mytest{
width:50px;
height:50px;
background-color:green;
position:absolute;
left:0px;
display:none;
font-size:12px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#do").click(function(){
$(".mytest").show(1000);
$(".mytest").animate({left:"+=200"},3000);
$(".mytest").animate({top:"+=50"},2000);
$(".mytest").queue(function(){$(this).text("動畫完成")});
})
$("#count").click(function(){
alert($(".mytest").queue().length)
})
})
</script>
</head>
<body>
<div class="box">
<div class="mytest"></div>
</div>
<button id="do">點擊開始動畫</button>
<button id="count">計算隊列中函數(shù)數(shù)量</button>
</body>
</html>
以上代碼實現(xiàn)了我們最終需要效果。
實例二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>queue()函數(shù)-腳本之家</title>
<style type="text/css">
.box{
width:300px;
height:150px;
}
.mytest{
width:50px;
height:50px;
background-color:green;
position:absolute;
left:0px;
display:none;
font-size:12px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#do").click(function(){
$(".mytest").show(1000);
$(".mytest").animate({left:"+=200"},3000);
$(".mytest").animate({top:"+=50"},2000);
$(".mytest").queue(function(){
$(this).text("動畫還將持續(xù)");
});
$(".mytest").animate({left:"-=200"},3000);
})
$("#count").click(function(){
alert($(".mytest").queue().length)
})
})
</script>
</head>
<body>
<div class="box">
<div class="mytest"></div>
</div>
<button id="do">點擊開始動畫</button>
<button id="count">計算隊列中函數(shù)數(shù)量</button>
</body>
</html>
以上代碼中,我們想在執(zhí)行完text()方法之后再執(zhí)行一個自定義動畫,但是表現(xiàn)卻并非如此,最后面的自定義動畫并沒有執(zhí)行。
代碼修改如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>queue()函數(shù)-腳本之家</title>
<style type="text/css">
.box{
width:300px;
height:150px;
}
.mytest{
width:50px;
height:50px;
background-color:green;
position:absolute;
left:0px;
display:none;
font-size:12px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#do").click(function(){
$(".mytest").show(1000);
$(".mytest").animate({left:"+=200"},3000);
$(".mytest").animate({top:"+=50"},2000);
$(".mytest").queue(function(){
$(this).text("動畫還將持續(xù)");
$(this).dequeue();
});
$(".mytest").animate({left:"-=200"},3000);
})
$("#count").click(function(){
alert($(".mytest").queue().length)
})
})
</script>
</head>
<body>
<div class="box">
<div class="mytest"></div>
</div>
<button id="do">點擊開始動畫</button>
<button id="count">計算隊列中函數(shù)數(shù)量</button>
</body>
</html>
以上代碼實現(xiàn)了我們的要求,在代碼中添加:
也就是說通過queue()添加函數(shù)時,我們應當確保最終調(diào)用了 .dequeue(),這樣下一個排隊的函數(shù)才能夠得到執(zhí)行。
希望本文所述對大家的jQuery程序設計有所幫助。
相關(guān)文章
基于jquery的一個OutlookBar類,動態(tài)創(chuàng)建導航條
初學jquery,如有錯誤,請高手們指出想看效果及完整代碼的可以下載rar包2010-11-11jQuery EasyUI API 中文文檔 - NumberBox數(shù)字框
jQuery EasyUI API 中文文檔 - NumberBox數(shù)字框使用介紹,需要的朋友可以參考下。2011-10-10jQuery移動頁面開發(fā)中的觸摸事件與虛擬鼠標事件簡介
這篇文章主要介紹了jQuery移動頁面開發(fā)中的觸摸事件與虛擬鼠標事件的簡單編寫方法,jQuery是當今人氣最高的Javascript庫并被廣泛應用于移動web的開發(fā),需要的朋友可以參考下2015-12-12Javascript jquery css 寫的簡單進度條控件
很多的時候用戶需要等待你“臃腫”的 Javascript 代碼處理完成(Web 2.0 的特色)。期間或許加入一個類似于進度條的東西讓用戶有點“安慰”。這個東西實現(xiàn)起來并不復雜,無非就是獲得總的處理條目,然后獲得一個百分比,再顯示輸出。2008-03-03jquery動態(tài)增加text元素以及刪除文本內(nèi)容實例代碼
這段代碼是通過jquery動態(tài)增加限定數(shù)額的text,以及清除文本內(nèi)容,用到了after()方法追加元素,具體實現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07