I found the jQuery.com document on queue()
/dequeue()
is too simple to understand. What exactly are queues in jQuery? How should I use them?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
This thread helped me a lot with my problem, but I've used $.queue in a different way and thought I would post what I came up with here. What I needed was a sequence of events (frames) to be triggered, but the sequence to be built dynamically. I have a variable number of placeholders, each of which should contain an animated sequence of images. The data is held in an array of arrays, so I loop through the arrays to build each sequence for each of the placeholders like this:
This is a simplified version of the script I have arrived at, but should show the principle - when a function is added to the queue, it is added using the Function constructor - this way the function can be written dynamically using variables from the loop(s). Note the way the function is passed the argument for the next() call, and this is invoked at the end. The function in this case has no time dependency (it doesn't use $.fadeIn or anything like that), so I stagger the frames using $.delay.
Function
makeRed
andmakeBlack
usequeue
anddequeue
to execute each other. The effect is that, the '#wow' element blinks continuously.To understand queue method, you 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.
Consider following code.
The 'queue'/'dequeue' method gives you control over this 'animation queue'.
By default the animation queue is named 'fx'. I have created a sample page here which has various examples which will illustrate how the queue method could be used.
http://jsbin.com/zoluge/1/edit?html,output
Code for above sample page:
Now you may ask, why should I bother with this queue? Normally, you wont. But if you have a complicated animation sequence which you want to control, then queue/dequeue methods are your friend.
Also see this interesting conversation on jQuery group about creating a complicated animation sequence.
http://groups.google.com/group/jquery-en/browse_thread/thread/b398ad505a9b0512/f4f3e841eab5f5a2?lnk=gst
Demo of the animation:
http://www.exfer.net/test/jquery/tabslide/
Let me know if you still have questions.
Multiple objects animation in a queue
Here is a simple example of multiple objects animation in a queue.
Jquery alow us to make queue over only one object. But within animation function we can access other objects. In this example we build our queue over #q object while animating #box1 and #box2 objects.
Think of queue as a array of functions. So you can manipulate queue as a array. You can use push, pop, unshift, shift to manipulate the queue. In this example we remove the last function from the animation queue and insert it at the beginning.
When we are done, we start animation queue by dequeue() function.
See at jsFiddle
html:
js:
css:
It allows you to queue up animations... for example, instead of this
Which fades the element and makes the width 100 px at the same time. Using the queue allows you to stage the animations. So one finishes after the other.
Example from http://docs.jquery.com/Effects/queue
The uses of jQuery
.queue()
and.dequeue()
Queues in jQuery are used for animations. You can use them for any purpose you like. They are an array of functions stored on a per element basis, using
jQuery.data()
. They are First-In-First-Out (FIFO). You can add a function to the queue by calling.queue()
, and you remove (by calling) the functions using.dequeue()
.To understand the internal jQuery queue functions, reading the source and looking at examples helps me out tremendously. One of the best examples of a queue function I've seen is
.delay()
:The default queue -
fx
The default queue in jQuery is
fx
. The default queue has some special properties that are not shared with other queues.$(elem).queue(function(){});
thefx
queue will automaticallydequeue
the next function and run it if the queue hasn't started.dequeue()
a function from thefx
queue, it willunshift()
(push into the first location of the array) the string"inprogress"
- which flags that the queue is currently being run.fx
queue is used by.animate()
and all functions that call it by default.NOTE: If you are using a custom queue, you must manually
.dequeue()
the functions, they will not auto start!Retrieving/Setting the queue
You can retrieve a reference to a jQuery queue by calling
.queue()
without a function argument. You can use the method if you want to see how many items are in the queue. You can usepush
,pop
,unshift
,shift
to manipulate the queue in place. You can replace the entire queue by passing an array to the.queue()
function.Quick Examples:
An animation (
fx
) queue example:Run example on jsFiddle
Another custom queue example
Run example on jsFiddle
Queueing Ajax Calls:
I developed an
$.ajaxQueue()
plugin that uses the$.Deferred
,.queue()
, and$.ajax()
to also pass back a promise that is resolved when the request completes. Another version of$.ajaxQueue
that still works in 1.4 is posted on my answer to Sequencing Ajax RequestsI have now added this as an article on learn.jquery.com, there are other great articles on that site about queues, go look.