|
14 | 14 | <example>
|
15 | 15 | <desc>Show the length of the queue.</desc>
|
16 | 16 | <code><![CDATA[
|
17 |
| -var div = $("div"); |
| 17 | +var div = $( "div" ); |
18 | 18 |
|
19 | 19 | function runIt() {
|
20 |
| - div.show("slow"); |
21 |
| - div.animate({left:'+=200'},2000); |
22 |
| - div.slideToggle(1000); |
23 |
| - div.slideToggle("fast"); |
24 |
| - div.animate({left:'-=200'},1500); |
25 |
| - div.hide("slow"); |
26 |
| - div.show(1200); |
27 |
| - div.slideUp("normal", runIt); |
| 20 | + div.show( "slow" ); |
| 21 | + div.animate({ left: "+=200" }, 2000 ); |
| 22 | + div.slideToggle( 1000 ); |
| 23 | + div.slideToggle( "fast" ); |
| 24 | + div.animate({ left: "-=200" }, 1500 ); |
| 25 | + div.hide( "slow" ); |
| 26 | + div.show( 1200 ); |
| 27 | + div.slideUp( "normal", runIt ); |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | function showIt() {
|
31 |
| - var n = div.queue("fx"); |
32 |
| - $("span").text( n.length ); |
33 |
| - setTimeout(showIt, 100); |
| 31 | + var n = div.queue( "fx" ); |
| 32 | + $( "span" ).text( n.length ); |
| 33 | + setTimeout( showIt, 100 ); |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | runIt();
|
37 | 37 | showIt();
|
38 | 38 | ]]></code>
|
39 |
| - <css><![CDATA[div { margin:3px; width:40px; height:40px; |
40 |
| - position:absolute; left:0px; top:60px; |
41 |
| - background:green; display:none; } |
42 |
| - div.newcolor { background:blue; } |
43 |
| - p { color:red; } ]]></css> |
| 39 | + <css><![CDATA[ |
| 40 | + div { |
| 41 | + margin: 3px; |
| 42 | + width: 40px; |
| 43 | + height: 40px; |
| 44 | + position: absolute; |
| 45 | + left: 0px; |
| 46 | + top: 60px; |
| 47 | + background: green; |
| 48 | + display: none; |
| 49 | + } |
| 50 | + div.newcolor { |
| 51 | + background: blue; |
| 52 | + } |
| 53 | + p { |
| 54 | + color: red; |
| 55 | + } |
| 56 | +]]></css> |
44 | 57 | <html><![CDATA[
|
45 |
| - <p>The queue length is: <span></span></p> |
46 |
| - <div></div>]]></html> |
| 58 | +<p>The queue length is: <span></span></p> |
| 59 | +<div></div> |
| 60 | +]]></html> |
47 | 61 | </example>
|
48 | 62 | <category slug="effects/custom-effects"/>
|
49 | 63 | <category slug="data"/>
|
@@ -72,80 +86,114 @@ showIt();
|
72 | 86 | <desc>Manipulate the queue of functions to be executed, once for each matched element.</desc>
|
73 | 87 | <longdesc>
|
74 | 88 | <p>Every element can have one to many queues of functions attached to it by jQuery. In most applications, only one queue (called <code>fx</code>) is used. Queues allow a sequence of actions to be called on an element asynchronously, without halting program execution. The typical example of this is calling multiple animation methods on an element. For example:</p>
|
75 |
| - <pre><code>$('#foo').slideUp().fadeIn();</code></pre> |
| 89 | + <pre><code> |
| 90 | +$( "#foo" ).slideUp().fadeIn(); |
| 91 | + </code></pre> |
76 | 92 | <p>When this statement is executed, the element begins its sliding animation immediately, but the fading transition is placed on the <code>fx</code> queue to be called only once the sliding transition is complete.</p>
|
77 | 93 | <p>The <code>.queue()</code> method allows us to directly manipulate this queue of functions. Calling <code>.queue()</code> with a callback is particularly useful; it allows us to place a new function at the end of the queue. The callback function is executed once for each element in the jQuery set.</p>
|
78 | 94 | <p>This feature is similar to providing a callback function with an animation method, but does not require the callback to be given at the time the animation is performed.</p>
|
79 |
| - <pre><code>$('#foo').slideUp(); |
80 |
| -$('#foo').queue(function() { |
81 |
| - alert('Animation complete.'); |
82 |
| - $(this).dequeue(); |
83 |
| -});</code></pre> |
| 95 | + <pre><code> |
| 96 | +$( "#foo" ).slideUp(); |
| 97 | +$( "#foo" ).queue(function() { |
| 98 | + alert( "Animation complete." ); |
| 99 | + $( this ).dequeue(); |
| 100 | +}); |
| 101 | + </code></pre> |
84 | 102 | <p>This is equivalent to:</p>
|
85 |
| - <pre><code>$('#foo').slideUp(function() { |
86 |
| - alert('Animation complete.'); |
87 |
| -});</code></pre> |
| 103 | + <pre><code> |
| 104 | +$( "#foo" ).slideUp(function() { |
| 105 | + alert( "Animation complete." ); |
| 106 | +}); |
| 107 | + </code></pre> |
88 | 108 | <p>Note that when adding a function with <code>.queue()</code>, we should ensure that <code>.dequeue()</code> is eventually called so that the next function in line executes.</p>
|
89 | 109 | <p><strong>As of jQuery 1.4</strong>, the function that's called is passed another function as the first argument. When called, this automatically dequeues the next item and keeps the queue moving. We use it as follows:</p>
|
90 |
| - <pre><code>$("#test").queue(function(next) { |
| 110 | + <pre><code> |
| 111 | +$( "#test" ).queue(function( next ) { |
91 | 112 | // Do some stuff...
|
92 | 113 | next();
|
93 |
| -});</code></pre> |
| 114 | +}); |
| 115 | + </code></pre> |
94 | 116 | </longdesc>
|
95 | 117 | <example>
|
96 | 118 | <desc>Queue a custom function.</desc>
|
97 |
| - <code><![CDATA[$(document.body).click(function () { |
98 |
| - $("div").show("slow"); |
99 |
| - $("div").animate({left:'+=200'},2000); |
100 |
| - $("div").queue(function () { |
101 |
| - $(this).addClass("newcolor"); |
102 |
| - $(this).dequeue(); |
103 |
| - }); |
104 |
| - $("div").animate({left:'-=200'},500); |
105 |
| - $("div").queue(function () { |
106 |
| - $(this).removeClass("newcolor"); |
107 |
| - $(this).dequeue(); |
108 |
| - }); |
109 |
| - $("div").slideUp(); |
110 |
| - });]]></code> |
| 119 | + <code><![CDATA[ |
| 120 | +$( document.body ).click(function() { |
| 121 | + $( "div" ).show( "slow" ); |
| 122 | + $( "div" ).animate({ left: "+=200" }, 2000 ); |
| 123 | + $( "div" ).queue(function() { |
| 124 | + $( this ).addClass( "newcolor" ); |
| 125 | + $( this ).dequeue(); |
| 126 | + }); |
| 127 | + $ ( "div" ).animate({ left: "-=200" }, 500 ); |
| 128 | + $( "div" ).queue(function() { |
| 129 | + $( this ).removeClass( "newcolor" ); |
| 130 | + $( this ).dequeue(); |
| 131 | + }); |
| 132 | + $( "div" ).slideUp(); |
| 133 | +}); |
| 134 | +]]></code> |
111 | 135 | <css><![CDATA[
|
112 |
| - div { margin:3px; width:40px; height:40px; |
113 |
| - position:absolute; left:0px; top:30px; |
114 |
| - background:green; display:none; } |
115 |
| - div.newcolor { background:blue; } |
116 |
| - ]]></css> |
117 |
| - <html><![CDATA[Click here... |
118 |
| - <div></div>]]></html> |
| 136 | + div { |
| 137 | + margin: 3px; |
| 138 | + width: 40px; |
| 139 | + height: 40px; |
| 140 | + position: absolute; |
| 141 | + left: 0px; |
| 142 | + top: 30px; |
| 143 | + background: green; |
| 144 | + display: none; |
| 145 | + } |
| 146 | + div.newcolor { |
| 147 | + background: blue; |
| 148 | + } |
| 149 | +]]></css> |
| 150 | + <html><![CDATA[ |
| 151 | +Click here... |
| 152 | +<div></div> |
| 153 | +]]></html> |
119 | 154 | </example>
|
120 | 155 | <example>
|
121 | 156 | <desc>Set a queue array to delete the queue.</desc>
|
122 |
| - <code><![CDATA[$("#start").click(function () { |
123 |
| - $("div").show("slow"); |
124 |
| - $("div").animate({left:'+=200'},5000); |
125 |
| - $("div").queue(function () { |
126 |
| - $(this).addClass("newcolor"); |
127 |
| - $(this).dequeue(); |
128 |
| - }); |
129 |
| - $("div").animate({left:'-=200'},1500); |
130 |
| - $("div").queue(function () { |
131 |
| - $(this).removeClass("newcolor"); |
132 |
| - $(this).dequeue(); |
133 |
| - }); |
134 |
| - $("div").slideUp(); |
135 |
| - }); |
136 |
| - $("#stop").click(function () { |
137 |
| - $("div").queue("fx", []); |
138 |
| - $("div").stop(); |
139 |
| - });]]></code> |
| 157 | + <code><![CDATA[ |
| 158 | +$( "#start" ).click(function() { |
| 159 | + $( "div" ).show( "slow "); |
| 160 | + $( "div" ).animate({ left: "+=200" }, 5000 ); |
| 161 | + $( "div" ).queue(function() { |
| 162 | + $( this ).addClass( "newcolor" ); |
| 163 | + $( this ).dequeue(); |
| 164 | + }); |
| 165 | + $( "div" ).animate({ left: '-=200' }, 1500 ); |
| 166 | + $( "div" ).queue(function() { |
| 167 | + $( this ).removeClass( "newcolor" ); |
| 168 | + $( this ).dequeue(); |
| 169 | + }); |
| 170 | + $( "div" ).slideUp(); |
| 171 | +}); |
| 172 | +$( "#stop" ).click(function() { |
| 173 | + $( "div" ).queue( "fx", [] ); |
| 174 | + $( "div" ).stop(); |
| 175 | +}); |
| 176 | +]]></code> |
140 | 177 | <css><![CDATA[
|
141 |
| - div { margin:3px; width:40px; height:40px; |
142 |
| - position:absolute; left:0px; top:30px; |
143 |
| - background:green; display:none; } |
144 |
| - div.newcolor { background:blue; } |
145 |
| - ]]></css> |
146 |
| - <html><![CDATA[<button id="start">Start</button> |
147 |
| - <button id="stop">Stop</button> |
148 |
| - <div></div>]]></html> |
| 178 | + div { |
| 179 | + margin: 3px; |
| 180 | + width: 40px; |
| 181 | + height: 40px; |
| 182 | + position: absolute; |
| 183 | + left: 0px; |
| 184 | + top: 30px; |
| 185 | + background: green; |
| 186 | + display: none; |
| 187 | + } |
| 188 | + div.newcolor { |
| 189 | + background: blue; |
| 190 | + } |
| 191 | +]]></css> |
| 192 | + <html><![CDATA[ |
| 193 | +<button id="start">Start</button> |
| 194 | +<button id="stop">Stop</button> |
| 195 | +<div></div> |
| 196 | +]]></html> |
149 | 197 | </example>
|
150 | 198 | <category slug="effects/custom-effects"/>
|
151 | 199 | <category slug="data"/>
|
|
0 commit comments