• If there’s some code that you don’t want to execute while the animation still
lasts, you can register it as a callb ack function. The callback function will be in-
voked automatically when the effect complete s. You cannot pass a ny arguments
to the callback function, but the this keyword will be accessible inside the
function referring to the element that was animated. So if you need to pass ad-
ditional information to the callback function, you can do that using the data()
method, which allows you to associate arbitrary data with an element.
• If you invoke an a nimation method on an already animated element, then the
new animation effect is put into an animation q ueue and is not anim ated until
all the effects before it are completed. Note, however, that each element has its
own animation queue.
There exist nine basic effects that jQuery d efines for hiding and showing elements, and
one method intended for custom animations. The simplest are effects that only animate
the CSS opacity property: fade In(), fadeOut(), and fadeTo(). These three
effects change the element’s visibility but don’t remove the elem ent from the page even
if it b ecomes completely invisible (when opacity becomes zero ). The fadeIn() and
fadeOut() methods animate opacity towards one a nd zero, respectively. However,
if you want to animate opacity to any other value, use the fadeTo() me thod, which
accepts the target opacity value as the second argument. The first argument is the
animation d uration, which you must always spe cify when c alling fadeTo().
If you want to remove the elemen t from the page layout (i.e., set the CSS display
property to none), then call hide(). This method gradually shrinks the element’s
width and height to zero and reduces its opacity to zero at the same time. If you
want to show the element again, then ca ll show( ), which reverses the process. By
default, hide() and show() do not animate the ch ange, but hide and show selected
elements instantly. If you want to get an animated effect, then pass an optiona l duration
argument. You can also toggle an element from visible to hidden and vice versa by
simply callin g toggle(). Tog gle effectively calls hid e() if the element is visible,
and it calls show() if the element is hidden.
The last thr ee of the predefined effects are slideUp(), slideDown(), and slide
Toggle(). slideUp () is similar to hide() but it o nly hides the element by ani-
mating its height to zero an d then setting its CSS display property to none. The
element’s width and opacity don’t change. The slideDown() property shows the el-
ement again by reversing the process. slideToggle( ), as its name suggests, toggle s
between slideUp() and slideDown ().
These are some examples of the basic animation methods in action:
$("p").fadeTo(500, 0.5); //Changes opacity to 0.5 in 500 ms
$("p").hide(500); //Hides all the paragraphs in 500 ms
$("p").show(); //Shows them back immediately
$("p").slideUp(); //Slides them up
$("p").slideToggle(function(){ //Registers a callback function
$(this).text("That’s all folks!"); //and slides the paragraphs
}); //back down
294 Appendix B. Ways to Continue