JQuery中的queue()及dequeue()

本文實例講述了jQuery中queue()方法用法。分享給你們供你們參考。具體分析以下:javascript

此方法可以顯示或者操做在匹配元素上執行的函數隊列。css

此方法可能用的並非太頻繁,可是卻很是的重要,下面就結合實例來介紹一下次方法的用法。
根據方法參數的不一樣,做用也有所不一樣。
說明:建議結合dequeue()函數一塊兒學習。
語法結構一:
html

代碼以下:java

$("selector").queue(queueName)jquery

參數列表:ide


參數 描述
queueName 可選。 第一個匹配元素上動畫隊列的名稱,默認值是「fx」。

沒有參數的時候,可以返回第一個匹配元素上的動畫隊列。函數

實例代碼:學習

代碼以下:動畫

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>queue()函數-腳本之家</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">計算隊列中函數數量</button>
</body>
</html>


因爲queue()函數沒有參數,因此返回值是第一個匹配元素上的動畫隊列,也就是div元素的動畫隊列,當點擊第二個按鈕的時候可以實時的計算出當前隊列中的動畫個數。
語法二:
this

代碼以下:

$("selector").queue(callback())

能夠爲匹配元素的函數隊列最後面添加一個函數。

參數列表:

參數 描述
callback() 匹配元素上的函數隊列最後面要添加的函數。

在語法一的實例中,你們可能注意到一個問題,那就是咱們但願在全部的動畫都完成以後,再在div中添加「動畫完成」四個字,可是從運行的實際表現來看,並不是如此,這主要的緣由是,show()和animate()動畫函數會默認的添加到fx動畫隊列中,而text()方法並不是動畫函數,因此不會加入到fx隊列,而且會首先執行。那麼能夠經過使用此函數,將text()方法入隊。

實例代碼:

實例一:

代碼以下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>queue()函數-腳本之家</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">計算隊列中函數數量</button>
</body>
</html>


以上代碼實現了咱們最終須要效果。

實例二:

代碼以下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>queue()函數-腳本之家</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("動畫還將持續");
    });
    $(".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">計算隊列中函數數量</button>
</body>
</html>

以上代碼中,咱們想在執行完text()方法以後再執行一個自定義動畫,可是表現卻並不是如此,最後面的自定義動畫並無執行。
代碼修改以下:

代碼以下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>queue()函數-腳本之家</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("動畫還將持續");
      $(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">計算隊列中函數數量</button>
</body>
</html>

以上代碼實現了咱們的要求,在代碼中添加:

代碼以下:

$(this).dequeue();

也就是說經過queue()添加函數時,咱們應當確保最終調用了 .dequeue(),這樣下一個排隊的函數纔可以獲得執行。

但願本文所述對你們的jQuery程序設計有所幫助。

相關文章
相關標籤/搜索