views:

56

answers:

2

Hello,

This is a code for a drop down menu. It is designed to drop-down (umdown) when the cursor moves over button. But when the cursor moves away from Multiple the umdown goes away.

HTML markup:

<div id="button"><span id="text">Down</span></div>

<div id="umdown">
    <div id="multi">Multiple</div>
    <div id="sd">Single</div>
</div>

CSS code:

#button{
    position:relative; top:1px; background-color:#060; width:200px; height:30px; background-image:url(../images/btn_bg.jpg); cursor:pointer;
}

#text{ position:absolute; margin-top:5px; text-align:center; width:200px; height:30px; font-stretch:expanded; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; color:#000; font-weight:bold; font-size:17px;}

#multi{
    width:160px; margin-left:5px;
}

#sd{
        width:160px; margin-left:5px;
}


#umdown{
    position:relative; left:30px; height:50px; background-color:#900; width:170px; cursor:pointer; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; color:#000; font-size:14px;
}

Javascript code:

$('#button').bind('mouseover',function(){
    $('#umdown').fadeIn();});
    $('#umdown').bind('mouseover',function(){
                $('#umdown').show();     });

    $('#umdown').bind('mouseout',function(){                                                                $('#umdown').fadeOut();
    });

Let me know what I have done wrong.

Thanks Jean

+2  A: 

Instead of mouseout here, you want mouseleave, so replace this:

$('#umdown').bind('mouseout',function(){
  $('#umdown').fadeOut();
});

With this:

$('#umdown').bind('mouseleave',function(){
  $(this).fadeOut();
});

The main difference is in the mouseleave docs:

The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element.

I would suggest using .stop() in this case as well to handle fade/hover issues, so your overall code looks like this:

$('#button').bind('mouseover',function(){
  $('#umdown').stop(true, true).fadeIn();
});
$('#umdown').bind('mouseleave',function(){
  $(this).stop(true, true).fadeOut();
});​

You can see a working demo here

Nick Craver
Not quite$('#umdown').bind('mouseleave',function(){ $('#umdown').fadeOut();});This works out....
Jean
@Jean - Clarify? Your comment doesn't make much sense...I added a demo of the code with your styling/markup to show the effect.
Nick Craver
I did it with $(this) it does not fadeOut(), so I kept the $('#umdown'), it fades out when the mouse leaves.
Jean
@nick Thanks for the `.stop(true,true)` this will help me alot.
adardesign
@nick, how different is the .stop() diff from the $('#umdown')?
Jean
@Jean - `.stop()` is separate from the selector, it stops the fade so the next animation can begin, so for example if you're hovering over it while it's fading in/out, the mouseenter/leave events don't happen because you were over an element that went away, causing a fading loop. Try the code without the `.stop()` and hover in/out fast to see what I mean.
Nick Craver
A: 

As i understand, you just want to toggle the visibility of #umdown when hovered over button.

$("#button").hover(function(){
  $("#umdown").fadeIn(500);
})


 $("#umdown").mouseleave(function(){
   $(this).fadeOut(300) 
});

See a working demo here

adardesign