jQuery中queue()方法用法实例
jQuery中queue()方法用法实例
发布时间:2016-12-30 来源:查字典编辑
摘要:本文实例讲述了jQuery中queue()方法用法。分享给大家供大家参考。具体分析如下:此方法能够显示或者操作在匹配元素上执行的函数队列。此...

本文实例讲述了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>

<div></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>

<div></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>

<div></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>

<div></div>

</div>

<button id="do">点击开始动画</button>

<button id="count">计算队列中函数数量</button>

</body>

</html>

以上代码实现了我们的要求,在代码中添加:

复制代码 代码如下:$(this).dequeue();

也就是说通过queue()添加函数时,我们应当确保最终调用了 .dequeue(),这样下一个排队的函数才能够得到执行。

希望本文所述对大家的jQuery程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类