views:

817

answers:

2

I am trying to create a menu with this structure:

<ul id="primary" class="menuOptions">
     <li>Item 1</li>
     <li>Item 2
         <div id="secondary">
            <ul class="menuOptions">
               <li>subItemOne</li>
               <li>subItemTwo</li>
           </ul>
         </div>
     </li>
     <li>Item 3</li>
</ul>

I am animating it with this script:

$j('.menuOptions li').each(function(i){
//applies to all menu options
    $j(this)
   .bind('mouseover',function() {
          $j(this).toggleClass("over");
       })
       .bind('mouseleave',function() {
          $j(this).toggleClass("over");
       });         
  });

I am finding that the toggleClass function is being called on the primary menu item as the mouse moves over the secondary menu items. I know that mouseleave is not supposed to be called until the mouse is off the children as well, so I am confused.

Do I need to actively stop event propagation somehow?

Thanks!

Tim

A: 

Can you try with just using the hover function.

$j('ul.menuOptions li').hover(function(){
          $j(this).toggleClass("over");
       } ,
       function() {
          $j(this).toggleClass("over");
       }
);
redsquare
The behavior is different but still not as desired. With my version, the primary item flashes as I move over each secondary. With your version, the primary toggles just once as the mouse leaves the primary area and moves onto the secondary area. So yours looks better, but still not what I want.
Tim Sheiner
Whoops! My mistake, I was testing on a bad example. Your solution does work. Thanks!
Tim Sheiner
no problem, good luck
redsquare
A: 

I suspect your problem was that you were coupling mouseover with mouseleave.

See http://api.jquery.com/category/events/mouse-events/

In general, you want to couple mouseenter with mouseleave, and mouseover with mouseout. mouseover and mouseout are older events which, while still occassionally useful, are generally inferior to mouseenter and mouseleave.

hover is exactly equivalent to using mouseenter and mouseleave.

Blinky