Home > Article > Web Front-end > Detailed explanation of jQuery.queue() instance usage
This function belongs to the jQuery
object (instance). If you need to remove and execute the first function in the queue, use the dequeue() function. You can also use the clearQueue() function to clear the specified queue.
jQuery 1.2 Add this function. queue()
The function has the following two usages:
Usage one:
jQueryObject.queue( [ queueName ] [, newQueue ] )
If no parameters are specified or only is specified The queueName
parameter means get the function queue with the specified name. If the newQueue
parameter is specified, it means using the new queue newQueue
to set (replace) all contents in the current queue.
Usage 2:
jQueryObject.queue( [ queueName ,] callback )
Add the specified function to the specified queue (end).
Note: All setting operations of the queue()
function are for each element matched by the current jQuery object; all read operations only Targets the first matching element.
parameter name defined in the previous syntax section.
Description | |
---|---|
Optional/String type The specified queue name, the default is "fx" (indicating the standard animation effect queue in jQuery). | |
Optional/Array typeA new queue used to replace the current queue contents. | |
Function typeThe function specified will be appended to the queue. This function has a function parameter, which can be called to remove and execute the first function in the queue. |
queue()The return value of the functionis the Array/jQuery type , the type of return value depends on whether the current queue()
function performs a get operation or a set operation.
queue() function performs a setting operation (replacing the queue, appending a function), it returns the current jQuery object itself; if it is a retrieval operation, it returns the obtained function queue ( array).
queue() function only uses the first matching element among them.
<span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n1"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span><span class="pln"><br/></span><span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n2"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span><span class="pln"><br/></span><span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n3"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span>We write the following jQuery code:
<span class="kwd">var</span><span class="pln"> $ps </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"p"</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="com">// 为每个p元素上的队列"q"设置(替换成)新的队列</span><span class="pln"><br/></span><span class="com">// (由于之前没有队列"q",这相当于新增一个对垒"q")</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数1"</span><span class="pun">);</span><span class="pln"> </span><span class="com">/* next(); 调用该函数可以移除并执行当前队列中的第一个函数 */</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">,</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数2"</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">,</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数3"</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln"><br/></span><span class="pun">]);</span><span class="pln"><br/><br/></span><span class="kwd">var</span><span class="pln"> queue </span><span class="pun">=</span><span class="pln"> $ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"> </span><span class="com">// 获取第一个p元素的队列"q"</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue1 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue2 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n2"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue3 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n3"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/><a href="http://www.php.cn/code/658.html" target="_blank">document</a></span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue1 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// true</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue2 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// false</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue3 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// false</span><span class="pln"><br/><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 3</span><span class="pln"><br/><br/></span><span class="com">// 为n1的队列"q"的末尾添加一个处理函数</span><span class="pln"><br/>$</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"><br/> </span><span class="com">// 这里的this表示当前DOM元素(n1)</span><span class="pln"><br/> alert</span><span class="pun">(</span><span class="str">"队列函数4"</span><span class="pun">);</span><span class="pln"> <br/></span><span class="pun">});</span><span class="pln"><br/><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 4</span><span class="pln"><br/><br/></span><span class="com">// 使用dequeue()可以移除并执行队列中的第一个函数</span><span class="pln"><br/>$</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">dequeue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"> </span><span class="com">// 弹出对话框:"队列函数1"</span>We can also not specify
queueNameParameter, the default value of this parameter is "fx", which represents jQuery's default effect queue.
<span class="kwd">var</span><span class="pln"> $ps </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"p"</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="com">// 为每个p元素设置两个自定义动画</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">animate</span><span class="pun">(</span><span class="pln"> </span><span class="pun">{</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">400</span><span class="pun">,</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">},</span><span class="pln"> </span><span class="lit">1000</span><span class="pln"> </span><span class="pun">)</span><span class="pln"><br/></span><span class="pun">.</span><span class="pln">animate</span><span class="pun">(</span><span class="pln"> </span><span class="pun">{</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">,</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> </span><span class="lit">1000</span><span class="pln"> </span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="kwd">var</span><span class="pln"> fx </span><span class="pun">=</span><span class="pln"> $ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">();</span><span class="pln"> </span><span class="com">// 相当于:var fx = $ps.queue("fx");</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 2</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<br>'</span><span class="pun">);</span><span class="pln"> </span><span class="com">// "inprogress"(第一个动画函数已被移除并开始执行,所以在队列开头添加该<a href="http://www.php.cn/wiki/57.html" target="_blank">字符串</a>来表示,如果执行完成,将开始移除并执行第二个动画函数)</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 第二个动画的执行函数</span><span class="pln"><br/><br/></span><span class="com">// 用一个空的数组替换当前动画队列,即可清空动画队列</span><span class="pln"><br/></span><span class="com">// 此时第一个动画函数已经从队列中移除、正在执行</span><span class="pln"><br/></span><span class="com">// 因此第一个动画函数执行完毕后,就不会执行第二个动画函数(被清空了)</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="pun">);</span>
The above is the detailed content of Detailed explanation of jQuery.queue() instance usage. For more information, please follow other related articles on the PHP Chinese website!