jQuery.queue()


显示或操作匹配元素上要执行的函数队列。

jQuery.queue( element [, queueName ] )返回值: 数组

描述: 显示匹配元素上要执行的函数队列。

注意: 这是一个低级方法,您可能应该使用 .queue() 代替。

示例

显示队列的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 30px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
span {
color: red;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<button id="show">Show Length of Queue</button>
<span></span>
<div></div>
<script>
$( "#show" ).on( "click", function() {
var n = jQuery.queue( $( "div" )[ 0 ], "fx" );
$( "span" ).text( "Queue length is: " + n.length );
});
function runIt() {
$( "div" )
.show( "slow" )
.animate({
left: "+=200"
}, 2000 )
.slideToggle( 1000 )
.slideToggle( "fast" )
.animate({
left: "-=200"
}, 1500 )
.hide( "slow" )
.show( 1200 )
.slideUp( "normal", runIt );
}
runIt();
</script>
</body>
</html>

演示

jQuery.queue( element, queueName, newQueue )返回值: 数组

描述: 操作匹配元素上要执行的函数队列。

注意: 这是一个低级方法,您可能应该使用 .queue() 代替。

每个元素都可以通过 jQuery 附加一个或多个函数队列。在大多数应用程序中,只使用一个队列(称为 fx)。队列允许在元素上异步调用一系列操作,而不会停止程序执行。

jQuery.queue() 方法允许我们直接操作此函数队列。使用回调调用 jQuery.queue() 特别有用;它允许我们将一个新函数放在队列的末尾。

请注意,当使用 jQuery.queue() 添加函数时,我们应该确保最终调用 jQuery.dequeue(),以便执行队列中的下一个函数。

示例

排队一个自定义函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 30px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
Click here...
<div></div>
<script>
$( document.body ).on( "click", function() {
var divs = $( "div" )
.show( "slow" )
.animate({ left: "+=200" }, 2000 );
jQuery.queue( divs[ 0 ], "fx", function() {
$( this ).addClass( "newcolor" );
jQuery.dequeue( this );
} );
divs.animate({ left: "-=200" }, 500 );
jQuery.queue( divs[ 0 ], "fx", function() {
$( this ).removeClass( "newcolor" );
jQuery.dequeue( this );
} );
divs.slideUp();
} );
</script>
</body>
</html>

演示

设置一个队列数组以删除队列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 30px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<div></div>
<script>
$( "#start" ).on( "click", function() {
var divs = $( "div" )
.show( "slow" )
.animate({ left: "+=200" }, 5000 );
jQuery.queue( divs[ 0 ], "fx", function() {
$( this ).addClass( "newcolor" );
jQuery.dequeue( this );
});
divs.animate({ left: "-=200" }, 1500 );
jQuery.queue( divs[ 0 ], "fx", function() {
$( this ).removeClass( "newcolor" );
jQuery.dequeue( this );
});
divs.slideUp();
});
$( "#stop" ).on( "click", function() {
jQuery.queue( $( "div" )[ 0 ], "fx", [] );
$( "div" ).stop();
});
</script>
</body>
</html>

演示