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

[ 95 ] Chapter 4 3. Save this file as slideFlicker.css, and add a link to the file from the of slideFlicker.html (directly after the link to fadeIn.css). 4. Finally, update the second element at the bottom of slideFlicker. html so that it appears as follows (new code shown in bold): var ul = $("nav ul"); ul.removeClass("purecss"); ul.find("a").each(function(){ if (!$(this).closest(".subnav").length) { var a = $(this); a.append("" + a.text() + ""); } }); ul.find("a").hover(function() { $(this).find("span").fadeIn("slow"); }, function() { $(this).find("span").hide(); }); $(".subnav", ul).parent().mouseenter(function() { $(this).find(".subnav").stop(true, true).slideDown("fast"); }); $(".subnav", ul).parent().mouseleave(function() { $(this).find(".subnav").stop(true, true).slideUp("fast"); }); What just happened? All we've done to the underlying HTML is added a couple of submenus to two of the top level list items in the . One of the submenus is a split menu made up of two elements inside a , the other is a single menu built from a standard . We've also added some new styling, mostly to carry on the theme from the original example. Some of the CSS is used to override previous rules set in the original stylesheet. Mostly the styling is purely for aesthetics, and we use a lot of CSS3 rounded-corner styling, which will not be apparent in all browsers.

Sliding Animations For the animations to work as intended, the submenus should initially be hidden from view with display:none. For the flicker effect to occur, the submenus should be wider than the parent that they are contained within. We've made one of our submenus wider, and the other one the same width so that we can easily see the difference. In the , we've added a simple check when the fading elements are added to the page so that the fade effect isn't applied to the submenus (it could easily be adapted to work, but we're looking at a different effect in this particular example). Following this we attach mouseenter and mouseleave effect handlers to the parents of any elements with the class subnav. In these handlers, we simply find the subnav within the element that triggered the event and either show or hide it with a slide effect. The stop() method is used to prevent animation build-up, as described in the previous chapter. Take a look at the page in a browser and note the difference between the two submenus: [ 96 ]

Sliding <strong>Animation</strong>s<br />

For the animations to work as intended, the submenus should initially be hidden from view<br />

with display:none. For the flicker effect to occur, the submenus should be wider than the<br />

parent that they are contained within. We've made one <strong>of</strong> our submenus wider, and<br />

the other one the same width so that we can easily see the difference.<br />

In the , we've added a simple check when the fading elements are added<br />

to the page so that the fade effect isn't applied to the submenus (it could easily be adapted<br />

to work, but we're looking at a different effect in this particular example).<br />

Following this we attach mouseenter and mouseleave effect handlers to the parents <strong>of</strong><br />

any elements with the class subnav. In these handlers, we simply find the subnav within<br />

the element that triggered the event and either show or hide it with a slide effect. The<br />

stop() method is used to prevent animation build-up, as described in the previous chapter.<br />

Take a look at the page in a browser and note the difference between the two submenus:<br />

[ 96 ]

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

Saved successfully!

Ooh no, something went wrong!