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

[ 47 ] Chapter 2 Usually these methods do not take any arguments, but we can trigger an animation by supplying an additional argument which represents either the duration, or the easing of the animation. We can also add a callback function in keeping with the animation methods we have looked at so far. The methods can take the following format: jQuery(elements).show(duration, [easing], [callback]); jQuery(elements).hide(duration, [easing], [callback]); These animations are a step-up in complexity from simple fading because instead of just animating the opacity of the selected elements, the width and height of the element are animated too. Syntactically, the methods are very similar however. This means that the selected elements will grow in size as they appear, or shrink as they disappear. We have little direct control over this however, and can set only the duration or easing of the animations. The direction in which the selected element will grow or shrink can be manipulated indirectly via CSS however. If the selected element is positioned using its left and top style properties, the animation will proceed from the top-left corner, growing to the right and down. If it is positioned using its right and bottom style properties, it will grow up and to the left instead. When an animation is not proceeding in the way in which you expect, it is always worth ruling out any interference from CSS. Styles can affect how an element is animated, so if you aren't getting errors in the JavaScript console, always check that the CSS isn't the cause of unexpected behavior. I have lost count of the hours spent checking and rechecking scripts, only to find that a tiny tweak to the CSS fixes the problem entirely. Like the fadeIn() method, elements that are to be animated using the show() method should initially be hidden with display:none. Elements being shown will be set to display:block, or whatever other display mode is acceptable for the element in question. Flyout submenus with jQuery's show/hide logic Let's take a look at a basic example of the show() and hide() methods in action. We'll create a simple vertical navigation menu which features fly-out submenus that are animated in and out of view. We'll look at the following aspects of these methods: How to trigger an animation using the show() or hide() methods

Fading Animations Our fly-out menu will be displayed like this: In the previous screenshot, we see the submenu fade in as well as grow outwards as the mouse pointer hovers over a top-level menu item. As before, the animation proceeds from the top to the bottom in the screenshot. [ 48 ]

[ 47 ]<br />

Chapter 2<br />

Usually these methods do not take any arguments, but we can trigger an animation by<br />

supplying an additional argument which represents either the duration, or the easing <strong>of</strong> the<br />

animation. We can also add a callback function in keeping with the animation methods we<br />

have looked at so far.<br />

The methods can take the following format:<br />

<strong>jQuery</strong>(elements).show(duration, [easing], [callback]);<br />

<strong>jQuery</strong>(elements).hide(duration, [easing], [callback]);<br />

These animations are a step-up in complexity from simple fading because instead <strong>of</strong> just<br />

animating the opacity <strong>of</strong> the selected elements, the width and height <strong>of</strong> the element are<br />

animated too. Syntactically, the methods are very similar however.<br />

This means that the selected elements will grow in size as they appear, or shrink as they<br />

disappear. We have little direct control over this however, and can set only the duration<br />

or easing <strong>of</strong> the animations.<br />

The direction in which the selected element will grow or shrink can be manipulated<br />

indirectly via CSS however. If the selected element is positioned using its left and top<br />

style properties, the animation will proceed from the top-left corner, growing to the right<br />

and down. If it is positioned using its right and bottom style properties, it will grow up<br />

and to the left instead.<br />

When an animation is not proceeding in the way in which you expect, it is always worth<br />

ruling out any interference from CSS. Styles can affect how an element is animated, so if<br />

you aren't getting errors in the JavaScript console, always check that the CSS isn't the cause<br />

<strong>of</strong> unexpected behavior. I have lost count <strong>of</strong> the hours spent checking and rechecking scripts,<br />

only to find that a tiny tweak to the CSS fixes the problem entirely.<br />

Like the fadeIn() method, elements that are to be animated using the show() method<br />

should initially be hidden with display:none. Elements being shown will be set to<br />

display:block, or whatever other display mode is acceptable for the element in question.<br />

Flyout submenus with <strong>jQuery</strong>'s show/hide logic<br />

Let's take a look at a basic example <strong>of</strong> the show() and hide() methods in action. We'll<br />

create a simple vertical navigation menu which features fly-out submenus that are animated<br />

in and out <strong>of</strong> view. We'll look at the following aspects <strong>of</strong> these methods:<br />

How to trigger an animation using the show() or hide() methods

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

Saved successfully!

Ooh no, something went wrong!