国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery中queue()方法用法實例

2024-05-06 16:13:06
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了jQuery中queue()方法用法,實例分析了queue()方法的功能、定義及使用技巧,非常具有實用價值,需要的朋友可以參考下
 
 

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

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

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

復制代碼代碼如下:
$("selector").queue(queueName)

 

參數列表:

 

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

 

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

實例代碼:

 

復制代碼代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<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://m.survivalescaperooms.com/" />
<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://m.survivalescaperooms.com/" />
<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://m.survivalescaperooms.com/" />
<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程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 五华县| 修武县| 天津市| 德化县| 鹤庆县| 吴忠市| 成安县| 司法| 大田县| 乳山市| 台江县| 金阳县| 抚松县| 长泰县| 宁陵县| 开阳县| 遂平县| 清流县| 丹阳市| 霸州市| 德保县| 顺义区| 洛扎县| 芷江| 工布江达县| 信宜市| 星子县| 常德市| 全南县| 清水县| 苗栗市| 贡山| 肥东县| 海宁市| 东莞市| 宜黄县| 贵南县| 蓬安县| 六安市| 济阳县| 岐山县|