Home  >  Article  >  Web Front-end  >  Detailed explanation of jQuery.queue() instance usage

Detailed explanation of jQuery.queue() instance usage

巴扎黑
巴扎黑Original
2017-06-20 11:11:261315browse


queue() function is used to get or set the function to be executed on the current matching element queue.

If the current jQuery object matches multiple elements: When getting the queue, only get the queue on the first matching element ; Set the queue (Replace queue, append function), then Each matching element is set separately.

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.

Syntax

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.

Parameters

Please find the corresponding parameter according to the

parameter name defined in the previous syntax section.

ParameterDescriptionqueueNamenewQueuecallback
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.
Return value

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.

If the

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).

If the current jQuery object matches multiple elements, when reading data, the

queue() function only uses the first matching element among them.

Example & Description

Take the following HTML code as an example:

<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">&#39;<br>&#39;</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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn