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程序设计有所帮助。

相关文章
相关标签/搜索