Description:
The jquery queue() method shows the queue of functions to be executed on the matched/selected elements. A queue is one or more functions waiting to run and it hasย several queues. Most often it has only one, the “fx” queue, which is the default jQuery queue.ย
For Better Understand:ย
It is anย array of functions stored on a per element basis, using jQuery.data(). They work as First-In-First-Out.ย To understand better, weย have to understand how jQuery does animation. If you write multiple animate method calls one after the other, jQuery creates an internal queue and adds these method calls to it. Then it runs those animate calls one by one.
Syntax
$(selector).queue(queueName)
queueNameย | Optional. Specifies the name of the queueย Default is “fx”, the standard effects queue |
Example:
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 |
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <script type="text/javascript"> $(document).ready(function () { $("button").click(function () { var divqueue = $("#divqueue"); divqueue.animate({ height: 100 }, "slow").animate({ width: 100 }, "slow") .animate({ height: 80 }, "slow").animate({ width: 80 }, "slow").animate({ margin: 80 }, "slow"); $("#divQueueLen").text(divqueue.queue().length); }); }); </script> </head> <body> <button>Queue Animation</button> length of the queue: <span id="divQueueLen"></span> <div id="divqueue" style="width:200px;height:200px;margin:50px; background-color:#4CAF50;"></div> </body> </html> |
Output:
On Click ofย “Click to start animation” button, the “divpanelBox” started to append (css stlyle) the new height & width for the same and started to increasing the size of the div as shown below.
Example:
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 |
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#btnStopAnimationQueue').click(function () { $('#divQueueBox').queue('fx',[]); }); $('#btnStartQueueAnimation').click(function () { $('#divQueueBox').queue(function () { $(this).animate({ height: '-=50', width: '-=50', "margin-left": '+=100' }, 1000); $(this).animate({ height: '-=50', width: '-=50', "margin-left": '+=100' }, 1000); $(this).animate({ height: '-=50', width: '-=50', "margin-left": '+=100' }, 1000); $(this).dequeue(); }); }); }); </script> <style type="text/css"> #divQueueBox { border: 1px solid red; width: 250px; height: 250px; position: absolute; background-color: #f00; left: 320px; } </style> </head> <body> <p><button id="btnStartQueueAnimation">start Queue animation.</button><br /></p> <div id='divQueueBox'> </div> </body> </html> |
Output:
Note :
After we build up our queue items, we have to call dequeue() on the queue in order to kick off the series of sequential events.deQueue()
method . we need to use dequeue() method with queue() elseย the queue will not be closed and you will get undesired result.
Leave A Comment