亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery中queue()方法用法實(shí)例

 更新時(shí)間:2014年12月29日 09:16:56   投稿:shichen2014  
這篇文章主要介紹了jQuery中queue()方法用法,實(shí)例分析了queue()方法的功能、定義及使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

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

此方法能夠顯示或者操作在匹配元素上執(zhí)行的函數(shù)隊(duì)列。

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

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

參數(shù)列表:

參數(shù) 描述
queueName 可選。 第一個(gè)匹配元素上動(dòng)畫隊(duì)列的名稱,默認(rèn)值是“fx”。

沒有參數(shù)的時(shí)候,能夠返回第一個(gè)匹配元素上的動(dòng)畫隊(duì)列。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.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("動(dòng)畫完成");
  })
  $("#count").click(function(){
    alert($(".mytest").queue().length)
  })
})
</script>
</head>
<body>
  <div class="box">
    <div class="mytest"></div>
  </div>
  <button id="do">點(diǎn)擊開始動(dòng)畫</button>
  <button id="count">計(jì)算隊(duì)列中函數(shù)數(shù)量</button>
</body>
</html>

由于queue()函數(shù)沒有參數(shù),所以返回值是第一個(gè)匹配元素上的動(dòng)畫隊(duì)列,也就是div元素的動(dòng)畫隊(duì)列,當(dāng)點(diǎn)擊第二個(gè)按鈕的時(shí)候能夠?qū)崟r(shí)的計(jì)算出當(dāng)前隊(duì)列中的動(dòng)畫個(gè)數(shù)。
語法二:

復(fù)制代碼 代碼如下:
$("selector").queue(callback())

可以為匹配元素的函數(shù)隊(duì)列最后面添加一個(gè)函數(shù)。

參數(shù)列表:

參數(shù) 描述
callback() 匹配元素上的函數(shù)隊(duì)列最后面要添加的函數(shù)。

在語法一的實(shí)例中,大家可能注意到一個(gè)問題,那就是我們希望在所有的動(dòng)畫都完成之后,再在div中添加“動(dòng)畫完成”四個(gè)字,但是從運(yùn)行的實(shí)際表現(xiàn)來看,并非如此,這主要的原因是,show()和animate()動(dòng)畫函數(shù)會(huì)默認(rèn)的添加到fx動(dòng)畫隊(duì)列中,而text()方法并非動(dòng)畫函數(shù),所以不會(huì)加入到fx隊(duì)列,并且會(huì)首先執(zhí)行。那么可以通過使用此函數(shù),將text()方法入隊(duì)。

實(shí)例代碼:

實(shí)例一:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.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("動(dòng)畫完成")});
  })
  $("#count").click(function(){
    alert($(".mytest").queue().length)
  })
})
</script>
</head>
<body>
<div class="box">
  <div class="mytest"></div>
</div>
<button id="do">點(diǎn)擊開始動(dòng)畫</button>
<button id="count">計(jì)算隊(duì)列中函數(shù)數(shù)量</button>
</body>
</html>

以上代碼實(shí)現(xiàn)了我們最終需要效果。

實(shí)例二:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.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("動(dòng)畫還將持續(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">點(diǎn)擊開始動(dòng)畫</button>
<button id="count">計(jì)算隊(duì)列中函數(shù)數(shù)量</button>
</body>
</html>

以上代碼中,我們想在執(zhí)行完text()方法之后再執(zhí)行一個(gè)自定義動(dòng)畫,但是表現(xiàn)卻并非如此,最后面的自定義動(dòng)畫并沒有執(zhí)行。
代碼修改如下:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.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("動(dòng)畫還將持續(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">點(diǎn)擊開始動(dòng)畫</button>
<button id="count">計(jì)算隊(duì)列中函數(shù)數(shù)量</button>
</body>
</html>

以上代碼實(shí)現(xiàn)了我們的要求,在代碼中添加:

復(fù)制代碼 代碼如下:
$(this).dequeue();

也就是說通過queue()添加函數(shù)時(shí),我們應(yīng)當(dāng)確保最終調(diào)用了 .dequeue(),這樣下一個(gè)排隊(duì)的函數(shù)才能夠得到執(zhí)行。

希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論