views:

25

answers:

2

im having trouble figuring out how to bind mouseout() to my entire nav bar including the links.

when a user hovers over a link in #nav a sub menu is shown. all is well there.

what i want to do is fadeOut that sub menu when the user hovers out of the entire #nav.

my code for the mouseout:

$('#nav').mouseout(function() {
  setTimeout(function() {
   //$('.sub-link').fadeOut();
  }, 2000);
});

when i hover over an anchor link which resides in #nav, i see the sub-menu. then i guess the mouseout() even fires and the sub-menu fades out. is there anyway to have the #nav and any anchor links within it to act as one?

i'd paste my markup but even indenting it 4 spaces still shows as rendered html..

sample page at: http://chrisparaiso.com/test/

TIA

A: 

Try using a selector like $('#nav, #nav > a')

Teja Kantamneni
+1  A: 

The mouseleave() should be what you need:

$('#nav').mouseleave(function() {
  setTimeout(function() {
   $('.sub-link').fadeOut();
  }, 2000);
});

From the documentation:

The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant.

You are right that mouseout is triggered if the cursor is over an element inside the element you bound the event to. But mouseleave is really only triggered when the cursor leaves the while element.

This example demonstrates very the the differences between mouseleave() and mouseout().

Felix Kling
I found this function minutes after posting my question.. it seemed to work at first, but then it just started doing the same thing. :(
centr0
@centr0: If you can provide a link to a sample page, we might be able to help further.
Felix Kling
sure thing. heres the sample page: http://chrisparaiso.com/test/
centr0
@centr0: Ok, I see. The problem is that the submenu is not contained in the navbar anymore (due to visibility stuff). An idea is to make `#nav` as heigh that it encapsulates the sub-menus and create the background color effect (the grey bar) with another `div` that surrounds the main menu list.
Felix Kling
Awesome! it worked. never thought it would be a css problem, but it makes sense! Thanks for taking the time. :)
centr0