1

I have a dropdown menu that works fine, but I would like it so, that if I hover off the menu, it doesn't immediately hide again. So basically I would like a one second delay.

I have read about setTimeout, but not sure if it is what I need?

$('#mainnav a').bind('mouseover', function()
{
    $(this).parents('li').children('ul').show();
});

$('#mainnav a').bind('mouseout', function()
{
    $(this).parents('li').children('ul').hide();
});

3 Answers 3

3

setTimeout is exactly what you need.

$('#mainnav a').bind('mouseout', function()
{
    var menu = this;
    setTimeout(function()
    {
        $(menu).parents('li').children('ul').hide();
    }, 1000);
});
Sign up to request clarification or add additional context in comments.

5 Comments

And the next time you show the menu, make sure this timer is off to prevent the menu from suddenly flipping on/off
thanks for the reply Bears, when I try it, the menu just stays now and doesn't disappear.
@Keith try setting: var menu = this; before setTimeout(), and change "this" into "menu" inside the timeout function
Thanks Baloo. Unfortunately the sub menu has a bit of a fit with your code :)
Ah yes, sorry about the this being off. Thanks baloo.
1

For mouseout I would add a chained animation before the hide() call:

$('#mainnav a').bind('mouseout', function()
{
    $(this).parents('li').children('ul').animate({opacity:0.99}, 2000).hide();
});

which would give a delay of 2 seconds.

Comments

0

Specify "slow" as a parameter to show and hide. From JQuery Docs.

$('#mainnav a').bind('mouseover', function()
{
    $(this).parents('li').children('ul').show("slow");
});

$('#mainnav a').bind('mouseout', function()
{
    $(this).parents('li').children('ul').hide("slow");
});

1 Comment

Not quite. That hides it slowly. It doesn't wait at all before hiding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.