jQuery中queue()方法用法執行個體_jquery

來源:互聯網
上載者:User

本文執行個體講述了jQuery中queue()方法用法。分享給大家供大家參考。具體分析如下:

此方法能夠顯示或者操作在匹配元素上執行的函數隊列。

此方法可能用的並不是太頻繁,但是卻非常的重要,下面就結合執行個體來介紹一下次方法的用法。
根據方法參數的不同,作用也有所不同。
說明:建議結合dequeue()函數一起學習。
文法結構一:

複製代碼 代碼如下:
$("selector").queue(queueName)

參數列表:

參數 描述
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元素的動畫隊列,當點擊第二個按鈕的時候能夠即時的計算出當前隊列中的動畫個數。
文法二:

複製代碼 代碼如下:
$("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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.