.queue()


顯示或操作要在配對元素上執行的函式佇列。

.queue( [queueName ] )傳回: 陣列

說明: 顯示要在配對元素上執行的函式佇列。

範例

顯示佇列長度。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>queue demo</title>
<style>
div {
margin: 3px;
width: 40px;
height: 40px;
position: absolute;
left: 0px;
top: 60px;
background: green;
display: none;
}
div.newcolor {
background: blue;
}
p {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<p>The queue length is: <span></span></p>
<div></div>
<script>
var div = $( "div" );
function runIt() {
div
.show( "slow" )
.animate({ left: "+=200" }, 2000 )
.slideToggle( 1000 )
.slideToggle( "fast" )
.animate({ left: "-=200" }, 1500 )
.hide( "slow" )
.show( 1200 )
.slideUp( "normal", runIt );
}
function showIt() {
var n = div.queue( "fx" );
$( "span" ).text( n.length );
setTimeout( showIt, 100 );
}
runIt();
showIt();
</script>
</body>
</html>

示範

.queue( [queueName ], newQueue )傳回: jQuery

說明:針對每個匹配的元素,操作要執行的函式佇列一次。

  • 新增版本:1.2.queue( [queueName ], newQueue )

    • queueName
      類型: 字串
      包含佇列名稱的字串。預設為 fx,即標準效果佇列。
    • newQueue
      類型:陣列
      用來取代目前佇列內容的函式陣列。
  • 新增版本:1.2.queue( [queueName ], callback )

    • queueName
      類型: 字串
      包含佇列名稱的字串。預設為 fx,即標準效果佇列。
    • callback
      類型:函式( 函式 next() )
      要新增到佇列的新函式,包含一個會將下一個項目出佇的函式呼叫。

jQuery 可以為每個元素附加一個到多個函式佇列。在大部分應用程式中,只會使用一個佇列(稱為 fx)。佇列允許以非同步方式在元素上呼叫一系列動作,而不會停止程式執行。典型的範例是在元素上呼叫多個動畫方法。例如

1
$( "#foo" ).slideUp().fadeIn();

執行此陳述式時,元素會立即開始其滑動動畫,但淡出轉場會放置在 fx 佇列中,只會在滑動轉場完成後才呼叫。

.queue() 方法允許我們直接操作此函式佇列。呼叫帶有回呼的 .queue() 特別有用;它允許我們將新函式置於佇列的尾端。回呼函式會針對 jQuery 集合中的每個元素執行一次。

此功能類似於提供動畫方法的回呼函式,但不需要在執行動畫時提供回呼。

1
2
3
4
5
$( "#foo" ).slideUp();
$( "#foo" ).queue(function() {
alert( "Animation complete." );
$( this ).dequeue();
});

這等於

1
2
3
$( "#foo" ).slideUp(function() {
alert( "Animation complete." );
});

請注意,當使用 .queue() 新增函數時,我們應確保最終會呼叫 .dequeue(),以便執行佇列中的下一個函數。

從 jQuery 1.4 開始,呼叫的函數會將另一個函數傳遞為第一個引數。呼叫時,這會自動將下一個項目出佇列,並保持佇列移動。我們使用它的方式如下

1
2
3
4
$( "#test" ).queue(function( next ) {
// Do some stuff...
next();
});

範例

將自訂函數排入佇列。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>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.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
Click here...
<div></div>
<script>
$( document.body ).on( "click", function() {
$( "div" )
.show( "slow" )
.animate( { left: "+=200" }, 2000 )
.queue(function() {
$( this ).addClass( "newcolor" ).dequeue();
})
.animate( { left: "-=200" }, 500 )
.queue(function() {
$( this ).removeClass( "newcolor" ).dequeue();
} )
.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>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.jquery.com/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() {
$( "div" )
.show( "slow" )
.animate({ left: "+=200" }, 5000 )
.queue(function() {
$( this ).addClass( "newcolor" ).dequeue();
})
.animate({ left: '-=200' }, 1500 )
.queue(function() {
$( this ).removeClass( "newcolor" ).dequeue();
})
.slideUp();
});
$( "#stop" ).on( "click", function() {
$( "div" )
.queue( "fx", [] )
.stop();
});
</script>
</body>
</html>

示範