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

[ 21 ] Chapter 2 We can control the duration of the animation using the duration argument to specify either an integer in milliseconds or one of the strings slow or fast, which are shortcuts for durations of 600 or 200 milliseconds, respectively. We can also supply 0 as the duration argument, which will effectively disable the animation. It's unlikely that we'd need to do this as it would be more efficient to not use an animation at all, but it is useful to know. I should point out that the fade will still occur; it will just happen over a duration of 0 milliseconds. The duration argument relates to the length of time the animation takes to run, not the speed of the animation. Therefore, a higher value will mean a slower, longer animation and not a faster, shorter animation. The easing argument can be changed from its default value of swing to linear, which causes the animation to progress at the same speed throughout the animation. The default, swing, causes the animation to start slowly, speed up slightly, and then slow down towards the end of the animation. The number of easing types can be greatly increased using plugins. We'll look at the extra easing types added by jQuery UI later in the book. We may supply a callback function (either a function reference or an anonymous function, with the latter being more common-place). This callback function will be executed after the animation ends for each element in the selection, so it may be triggered more than once if more than a single element is being animated. jQuery's Unified Animation API In addition to the fading methods looked at in this chapter, it is worth noting at this stage that any of the animation methods exposed by jQuery can accept the strings slow or fast as the value of the duration argument, and they will always equate to durations of 600 or 200 milliseconds, respectively. Any other strings that are supplied will be ignored and will cause the animation to take its default duration of 400 milliseconds (as if no duration argument had been supplied). Any integer, representing an actual length of time in milliseconds, can also be supplied. Additionally, all jQuery animation methods can have easing configured, and all can accept a callback function as an argument. Each of the animation methods work and are used in the same way; it is just the effect which differs. This makes using the methods easy and intuitive.

Fading Animations A useful point to note is that the arguments do not need to all be provided when only the callback function is required. If we don't need to set the duration or easing arguments, we can just supply the callback function and jQuery will still execute it at the end of the animation. Enhancing simple CSS hover states with fadeIn It is standard practice to add hover-states for the items in a navigation menu using CSS, but with jQuery we can progressively enhance simple CSS hover states into full-on animations that give a much more attractive and professional effect when hovered. In this example, we will see how to: Disable standard CSS hovers when JavaScript is enabled Add the additional HTML markup required for the animations Implement attractive fadeIn() animations triggered by hovering We'll end up with a navigation menu in which the hover-states are animated into view instead of being shown instantly, as shown in the following screenshot: [ 22 ]

[ 21 ]<br />

Chapter 2<br />

We can control the duration <strong>of</strong> the animation using the duration argument to specify either<br />

an integer in milliseconds or one <strong>of</strong> the strings slow or fast, which are shortcuts for<br />

durations <strong>of</strong> 600 or 200 milliseconds, respectively.<br />

We can also supply 0 as the duration argument, which will effectively disable the animation.<br />

It's unlikely that we'd need to do this as it would be more efficient to not use an animation at<br />

all, but it is useful to know. I should point out that the fade will still occur; it will just happen<br />

over a duration <strong>of</strong> 0 milliseconds.<br />

The duration argument relates to the length <strong>of</strong> time the animation takes to run,<br />

not the speed <strong>of</strong> the animation. Therefore, a higher value will mean a slower,<br />

longer animation and not a faster, shorter animation.<br />

The easing argument can be changed from its default value <strong>of</strong> swing to linear, which<br />

causes the animation to progress at the same speed throughout the animation. The default,<br />

swing, causes the animation to start slowly, speed up slightly, and then slow down towards<br />

the end <strong>of</strong> the animation.<br />

The number <strong>of</strong> easing types can be greatly increased using plugins. We'll look<br />

at the extra easing types added by <strong>jQuery</strong> UI later in the book.<br />

We may supply a callback function (either a function reference or an anonymous function,<br />

with the latter being more common-place). This callback function will be executed after the<br />

animation ends for each element in the selection, so it may be triggered more than once if<br />

more than a single element is being animated.<br />

<strong>jQuery</strong>'s Unified <strong>Animation</strong> API<br />

In addition to the fading methods looked at in this chapter, it is worth noting at this stage<br />

that any <strong>of</strong> the animation methods exposed by <strong>jQuery</strong> can accept the strings slow or fast<br />

as the value <strong>of</strong> the duration argument, and they will always equate to durations <strong>of</strong> 600 or<br />

200 milliseconds, respectively.<br />

Any other strings that are supplied will be ignored and will cause the animation to take its<br />

default duration <strong>of</strong> 400 milliseconds (as if no duration argument had been supplied). Any<br />

integer, representing an actual length <strong>of</strong> time in milliseconds, can also be supplied.<br />

Additionally, all <strong>jQuery</strong> animation methods can have easing configured, and all can accept a<br />

callback function as an argument. Each <strong>of</strong> the animation methods work and are used in the<br />

same way; it is just the effect which differs. This makes using the methods easy and intuitive.

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

Saved successfully!

Ooh no, something went wrong!