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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In the previous screenshot, we see both submenus in their expanded, visible states.<br />

[ 97 ]<br />

Chapter 4<br />

The flicker problem arises in part because <strong>of</strong> the visitor's perception <strong>of</strong> where the submenu<br />

actually is. It is quite clear with the thin submenu where the boundaries <strong>of</strong> the menu are.<br />

The visitor will most likely move their mouse pointer straight down into the submenu when<br />

it appears on the page.<br />

With the wider submenu, the visitor may not be quite as sure where they need to move their<br />

mouse in order to enter the submenu. Instead <strong>of</strong> moving straight down from the Articles<br />

top-level item into the submenu, they may instead move their mouse pointer diagonally<br />

down and to the right, thinking that they can enter the second list <strong>of</strong> links that way.<br />

It is this behavior that produces the flicker effect; try it out yourself—move the mouse<br />

pointer diagonally down and to the right when entering the wide submenu. The submenu<br />

should flicker on and <strong>of</strong>f wildly.<br />

Time for action – fixing the flicker<br />

In this section we'll see how to prevent the flicker from spoiling the animation.<br />

1. Fixing the problem is relatively easy. Simply update the JavaScript so that it appears<br />

as follows (new code again shown in bold):<br />

var ul = $("nav ul"),<br />

timer = null;<br />

ul.removeClass("purecss");<br />

ul.find("a").each(function(){<br />

if (!$(this).closest(".subnav").length) {<br />

var a = $(this);<br />

a.append("" + a.text() + "");<br />

}<br />

});<br />

ul.find("a").hover(function() {<br />

$(this).find("span").fadeIn("slow");<br />

}, function() {<br />

$(this).find("span").hide();<br />

});<br />

$(".subnav", ul).parent().mouseenter(function() {<br />

clearTimeout(timer);<br />

$(this).find(".subnav").stop(true, true).slideDown("fast");<br />

});

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

Saved successfully!

Ooh no, something went wrong!