02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Managing <strong>Animation</strong>s<br />

Time for action – preventing animation build-up using<br />

the stop method<br />

In Chapter 2, Fading <strong>Animation</strong>s, we used the fadeIn() method to enhance CSS hover<br />

states, but we didn't hide the hover state using fadeOut(). The reason for this is because<br />

the animations can quickly build up. If the mouse pointer is moved on and <strong>of</strong>f one <strong>of</strong> the<br />

links repeatedly, the hover state will continue to fade in and out even after the mouse<br />

pointer moves away.<br />

Fortunately, we can use the stop() method to prevent this from happening. In this section,<br />

we'll add fadeOut() effects to the navigation menu example from Chapter 2, Fading<br />

<strong>Animation</strong>s and use the stop() method to prevent an effect build-up.<br />

1. In fadeIn.html, change the hover() method so that it appears as follows:<br />

ul.find("a").hover(function() {<br />

$(this).find("span").stop(true, true).fadeIn("slow");<br />

}, function() {<br />

$(this).find("span").stop(true, true).fadeOut("slow");<br />

});<br />

2. Save this file as stop.html.<br />

What just happened?<br />

By calling the stop() method directly before applying the fadeIn() effect, we ensure that<br />

a build-up <strong>of</strong> effects does not occur and spoil the hover states. In order for the effects to work<br />

correctly, we supply true as the values <strong>of</strong> both the clear queue and jump to end arguments.<br />

Pop quiz – stopping an animation<br />

1. What does the first argument that can be passed to the stop() method determine?<br />

a. Whether or not the method should return false<br />

b. Whether or not the element should be removed from the page<br />

c. Whether or not the queue should be cleared<br />

d. Whether or not the queue should be replaced<br />

2. What does the second argument control?<br />

a. Whether or not the queue is cleared<br />

b. Whether or not the method returns the queue as an array<br />

c. Whether the element should be removed from the page<br />

d. Whether the element should be set to its final state<br />

[ 68 ]

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

Saved successfully!

Ooh no, something went wrong!