jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

[ 65 ] Chapter 3 We pass a callback function into the fadeIn() method used with the new . In this function, we just call the dequeue() method. This is required for the next function in the queue to be executed; if we don't do this, only the first function in our custom queue will be executed. Next we define our replacement queue, after first caching a selector for the #fader element. The new queue is defined as an array where each item consists of an anonymous function which in turn invokes our colorChange() function, passing in the cached selector and a CSS color name. Finally, we call an effect method on the target element and queue up some additional effects as we did before. This time when we call the queue() method, we supply our custom queue, which replaces the default fx queue created by the chained fade methods. When we run the page in a browser, we see that the first effect is applied, and then our queue of custom colorChange functions is called. The two fade effects that would have been in the default fx queue originally are not executed. Pop quiz – replacing the queue 1. What do we need to pass to the queue() method in order to replace the queue? a. A string containing a function reference b. The dequeue() method c. An array d. A Boolean 2. What is an easy way of clearing the default fx queue, other than using the clearQueue() method? a. Passing the string clear to the queue() method b. Passing the integer 0 to the queue() method c. Passing the Boolean false to the queue() method d. Passing an empty array to the queue() method Ensuring custom queues iterate correctly When we create custom queues, the chained methods are not automatically called for us. This is something we need to do manually and is handled using the dequeue() method as we saw in the previous example.

Download from Wow! eBook Managing Animations When called, it will remove the next function in the queue and execute it. It's a simple method, with few arguments, and is used in a very specific manner. The method may take a single optional argument which is the name of the queue to execute the next function from: jQuery(elements).dequeue([queue name]); The queue name is only required if we are working with a queue other than the default fx queue. We didn't need to provide the name of the queue in the previous example in the last section because we replaced the animated element's default fx queue. The dequeue() method has the same effect as calling the next() function that we used to keep the queue moving in the queueInsert.html example from the last section. Time for action – dequeueing functions Let's change the queueInsert.html page so that it uses the dequeue() method instead of the next() function to keep the queue moving. 1. Change the code in queueAdd.html so that it appears as follows: $("#fader").fadeOut(function() { $(this).queue(function() { $(this).css("backgroundColor", "green").dequeue(); }).fadeOut(); }).fadeIn().fadeOut().fadeIn(); 2. Save this version as dequeue.html. What just happened? This time we do not need to pass anything into the callback function passed to the queue() method. We simply chain the dequeue() method to the after setting its background-color to green. This has the same effect as before and the green will fade out at the end of the animation. Stopping an animation The stop() method can be used to stop an effect that is currently running on the selected element. In its simplest form, we may call the method without supplying any additional arguments, but if necessary we can supply up to two Boolean arguments. The method takes the following format: jQuery(elements).stop([clear queue], [jump to end]); [ 66 ]

[ 65 ]<br />

Chapter 3<br />

We pass a callback function into the fadeIn() method used with the new . In this<br />

function, we just call the dequeue() method. This is required for the next function in the<br />

queue to be executed; if we don't do this, only the first function in our custom queue will<br />

be executed.<br />

Next we define our replacement queue, after first caching a selector for the #fader<br />

element. The new queue is defined as an array where each item consists <strong>of</strong> an anonymous<br />

function which in turn invokes our colorChange() function, passing in the cached selector<br />

and a CSS color name.<br />

Finally, we call an effect method on the target element and queue up some additional<br />

effects as we did before. This time when we call the queue() method, we supply our<br />

custom queue, which replaces the default fx queue created by the chained fade methods.<br />

When we run the page in a browser, we see that the first effect is applied, and then our<br />

queue <strong>of</strong> custom colorChange functions is called. The two fade effects that would have<br />

been in the default fx queue originally are not executed.<br />

Pop quiz – replacing the queue<br />

1. What do we need to pass to the queue() method in order to replace the queue?<br />

a. A string containing a function reference<br />

b. The dequeue() method<br />

c. An array<br />

d. A Boolean<br />

2. What is an easy way <strong>of</strong> clearing the default fx queue, other than using the<br />

clearQueue() method?<br />

a. Passing the string clear to the queue() method<br />

b. Passing the integer 0 to the queue() method<br />

c. Passing the Boolean false to the queue() method<br />

d. Passing an empty array to the queue() method<br />

Ensuring custom queues iterate correctly<br />

When we create custom queues, the chained methods are not automatically called for us.<br />

This is something we need to do manually and is handled using the dequeue() method as<br />

we saw in the previous example.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!