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

Download from Wow! eBook [ 51 ] Chapter 2 What just happened? The simple markup for this example consists of the container element and a series of nested unordered lists. As I mentioned, the way in which the selected element grows when it is acted upon by the show() method is dictated by the CSS positioning that it uses. In this case, we'd like the menu to grow down and to the right, so we position the submenus using their top and left properties. The submenus are also hidden initially with display:none; as is required for an element to be shown with the show() method. In the script, we first select each of the nested elements and then navigate up to their respective parent elements. We then use the mouseleave() and mouseenter() event helper method to attach the events. This is so that only list items containing submenus will have the listeners attached. In the anonymous functions supplied to these listeners, we navigate down to the nested and call the show() or hide() method as appropriate, triggering an animation by supplying the string fast. When we run this page in a browser, we should find that hovering the mouse pointer over one of the list items that contain a submenu cause the submenu to be animated into full size and opacity. Pop quiz – using show and hide 1. Which argument should be provided to trigger animations when using the show() or hide() methods? a. A callback function b. An object containing the duration and easing for the animation c. An integer or string specifying the duration of the animation, and/or a string specifying the easing d. An array containing the duration and easing for the animation 2. How is the direction of growth in the animated element controlled? a. By its CSS positioning properties b. By setting a property of the jQuery object c. By passing an object containing values for left and top properties into the method d. It cannot be controlled

Fading Animations Animated toggling jQuery provides a toggle() method, which can be used in several different ways depending on which arguments are passed to it. Normally it is used to execute two or more functions alternately, but like the show() or hide() methods, it can be used to create animations when a duration or easing is supplied as an argument. When animations are required from the toggle() method, we should simply supply a duration and/or an easing argument, which may take the same numerical or string based format as the other animation methods we have looked at so far. The same rules about CSS positioning also apply to the toggle() method, so the direction of animation is easy to customize. The duration argument may be of string or integer types, and a callback function may be provided if required. The easing argument should be in string format. The toggle() method works in a similar way to the fadeToggle() method that we looked at earlier in the chapter. It contains logic that checks the current state of the element and either shows, or hides the element based on this. The method should be seen merely as a convenient short-cut that may be beneficial in some basic situations. From a performance perspective, the show() and hide() methods are marginally more efficient as there is no internal check on the current visibility of the element being animated. It is effectively a combination of the show() and hide() methods. Time for action – replacing show and hide with toggle In this example, we'll change our showHide.html page so that the submenus are displayed (or hidden) with toggle() instead of using show() and hide(). 1. All that needs to change is the script: subMenuParent.mouseenter(function() { }); $(this).find("ul").toggle("fast"); subMenuParent.mouseleave(function() { }); $(this).find("ul").toggle("fast"); [ 52 ]

Download from Wow! eBook <br />

[ 51 ]<br />

Chapter 2<br />

What just happened?<br />

The simple markup for this example consists <strong>of</strong> the container element and a series <strong>of</strong><br />

nested unordered lists.<br />

As I mentioned, the way in which the selected element grows when it is acted upon by the<br />

show() method is dictated by the CSS positioning that it uses. In this case, we'd like the<br />

menu to grow down and to the right, so we position the submenus using their top and left<br />

properties. The submenus are also hidden initially with display:none; as is required for<br />

an element to be shown with the show() method.<br />

In the script, we first select each <strong>of</strong> the nested elements and then navigate up to their<br />

respective parent elements. We then use the mouseleave() and mouseenter()<br />

event helper method to attach the events. This is so that only list items containing submenus<br />

will have the listeners attached.<br />

In the anonymous functions supplied to these listeners, we navigate down to the nested<br />

and call the show() or hide() method as appropriate, triggering an animation by<br />

supplying the string fast.<br />

When we run this page in a browser, we should find that hovering the mouse pointer over<br />

one <strong>of</strong> the list items that contain a submenu cause the submenu to be animated into full size<br />

and opacity.<br />

Pop quiz – using show and hide<br />

1. Which argument should be provided to trigger animations when using the show()<br />

or hide() methods?<br />

a. A callback function<br />

b. An object containing the duration and easing for the animation<br />

c. An integer or string specifying the duration <strong>of</strong> the animation, and/or a string<br />

specifying the easing<br />

d. An array containing the duration and easing for the animation<br />

2. How is the direction <strong>of</strong> growth in the animated element controlled?<br />

a. By its CSS positioning properties<br />

b. By setting a property <strong>of</strong> the <strong>jQuery</strong> object<br />

c. By passing an object containing values for left and top properties into<br />

the method<br />

d. It cannot be controlled

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

Saved successfully!

Ooh no, something went wrong!