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.

Fading <strong>Animation</strong>s<br />

In the previous screenshot, we see our help text gradually fading into view. Once the icon is<br />

clicked a second time, the animation will be shown in reverse, with the element going from<br />

full opacity to full transparency.<br />

Pop quiz – using fadeToggle()<br />

1. Why should we use caution when using fadeToggle()?<br />

a. Because elements still affect the flow <strong>of</strong> the page once they have been<br />

faded out<br />

b. Because <strong>of</strong> the black border problem in Internet Explorer<br />

c. Because the natural display style <strong>of</strong> an element is not always maintained,<br />

so inline elements will be set to display:block when they are faded in<br />

d. Because the method does not check the current state <strong>of</strong> the element it is<br />

called on<br />

Have a go hero – extending fadeToggle()<br />

In this example we faded an element that has an alpha-transparent PNG as its background<br />

image. This means that the page will suffer from the black border problem in all current<br />

versions <strong>of</strong> IE. Have a go at using one <strong>of</strong> the fixes described earlier in the chapter to fix<br />

the problem.<br />

Greater opacity control with fadeTo()<br />

With the fadeTo() method we can use the same arguments as before, but we can also<br />

control the final opacity <strong>of</strong> the element that is being animated. This is very useful for<br />

situations where we don't want to fade an element all the way in or out.<br />

Unlike the fadeIn() or fadeOut() methods, the fadeTo() method must be supplied<br />

with both a duration and an ending opacity at least. The duration argument accepts the<br />

same values as with the fadeIn(), fadeToggle(), and fadeOut() methods.<br />

The ending opacity is provided as the second argument and should be a decimal number<br />

between the integers 0 and 1, which represents the percentage <strong>of</strong> opacity, with 0 being fully<br />

transparent, and 1 being fully opaque. 50% opacity is therefore specified as 0.5.<br />

If easing is required, this should be provided as the third argument and can take the strings<br />

swing (the default) or linear, like the other methods we have looked at so far. A callback<br />

function to be executed for each selected element may also be supplied.<br />

[ 36 ]

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

Saved successfully!

Ooh no, something went wrong!