jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 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 ]
- Page 35 and 36: Introduction In the previous screen
- Page 37 and 38: Introduction Once our variables hav
- Page 40 and 41: 2 Fading Animations In this chapter
- Page 42 and 43: [ 21 ] Chapter 2 We can control the
- Page 44 and 45: The previous screenshot shows how t
- Page 46 and 47: [ 25 ] Chapter 2 Most of the styles
- Page 48 and 49: 2. What else can be passed into the
- Page 50 and 51: Time for action - creating the dial
- Page 52 and 53: [ 31 ] Chapter 2 For example, the a
- Page 54 and 55: Download from Wow! eBook The follo
- Page 56 and 57: [ 35 ] Chapter 2 The CSS is used mo
- Page 58 and 59: The fadeTo() method is used in the
- Page 60 and 61: [ 39 ] Chapter 2 #messageList table
- Page 62 and 63: [ 41 ] Chapter 2 What just happened
- Page 64 and 65: 2. What format can these arguments
- Page 66 and 67: } var el = $(this); if (el.css("fil
- Page 68 and 69: [ 47 ] Chapter 2 Usually these meth
- Page 70 and 71: Time for action - animations with s
- Page 72 and 73: Download from Wow! eBook [ 51 ] Ch
- Page 74 and 75: 2. Save the changes as toggle.html.
- Page 76 and 77: 3 Managing Animations Like most of
- Page 78 and 79: [ 57 ] Chapter 3 A custom queue tha
- Page 80 and 81: If we wish to see how many function
- Page 82 and 83: 2. What does the queue() method ret
- Page 84 and 85: [ 63 ] Chapter 3 The argument we su
- Page 88 and 89: [ 67 ] Chapter 3 The first argument
- Page 90 and 91: Delaying queue execution [ 69 ] Cha
- Page 92 and 93: [ 71 ] Chapter 3 Also note that the
- Page 94 and 95: 4 Sliding Animations Another type o
- Page 96 and 97: Time for action - creating a slide-
- Page 98 and 99: [ 77 ] Chapter 4 header form input
- Page 100 and 101: Pop quiz - sliding elements down 1.
- Page 102 and 103: Download from Wow! eBook }); }); $
- Page 104 and 105: Toggling the slide [ 83 ] Chapter 4
- Page 106 and 107: [ 85 ] Chapter 4 } h2 { margin:0; p
- Page 108 and 109: [ 87 ] Chapter 4 The previous scree
- Page 110 and 111: Pop quiz - using slideToggle 1. Wha
- Page 112 and 113: [ 91 ] Chapter 4 Easing is a great
- Page 114 and 115: [ 93 ] Chapter 4 Home Articles J
- Page 116 and 117: [ 95 ] Chapter 4 3. Save this file
- Page 118 and 119: In the previous screenshot, we see
- Page 120 and 121: Pop quiz - fixing the flicker 1. Wh
- Page 122 and 123: 5 Custom Animations The predefined
- Page 124 and 125: An alternative syntax for animate()
- Page 126 and 127: [ 105 ] Chapter 5 width:295px; marg
- Page 128 and 129: [ 107 ] Chapter 5 What just happene
- Page 130 and 131: [ 109 ] Chapter 5 Although not esse
- Page 132 and 133: [ 111 ] Chapter 5 The previous scre
- Page 134 and 135: [ 113 ] Chapter 5 The styles we use
[ 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.