Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery delegate() events (mouseover mouseout) fires twice

i have the following script firing mouseover and mouseout always twice! what do you suggest i do wrong (unbind, return e.g.)? i tried a few things but nothing helped.

here is the code:

  $('#container').delegate('div.showmenu', 'mouseover mouseenter mouseout mouseleave', function(e){
  if (e.type === 'mouseover' || e.type==='mouseenter') { //jIE requires mouseenter, does not fire mouseover                                 
        if($(this).parents().closest('div').hasClass('whatever')){            
          alert(e.type);  //double-alerts mouseover

          menu.show();

    foldercmenu.hover(
        function(){
            $(this).show();                             
        },
        function(){
            $(this).hide();                                             
        }
    );                              

        }else {
    //do other stuff :-)
    }                                               
  }else if(e.type==='mouseout' || e.type==='mouseleave'){  //IE requires mouseleave, does not fire mouseout  
        alert(e.type);  //double-alerts mouseout
        menu.hide();
        $(this).unbind('mouseover mouseenter mouseout mouseleave');
  }
  //return false;   
});
like image 223
s.hoff Avatar asked Sep 08 '11 14:09

s.hoff


People also ask

What is mouseover and Mouseout?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves. These events are special, because they have property relatedTarget . This property complements target . When a mouse leaves one element for another, one of them becomes target , and the other one – relatedTarget .

What is Mouseout event?

The mouseout event is fired at an Element when a pointing device (usually a mouse) is used to move the cursor so that it is no longer contained within the element or one of its children.

What is Mouseleave event?

The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.

What is jQuery mouseover?

jQuery mouseover() MethodThe mouseover event occurs when the mouse pointer is over the selected element. The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs.


1 Answers

mouseover and mouseout a triggered when you enter/leave a child of the element, maybe that's the effect you are seeing.

An other problem is that you are binding the handler to both, mouseover and mouseenter ( and mouseleave and mouseout).

Only bind to mouseenter and mouseleave. jQuery is already taking care of the browser differences.

like image 200
Felix Kling Avatar answered Sep 22 '22 05:09

Felix Kling