How to delay hiding of a menu with Jquery Dropdown Menu?

杀马特。学长 韩版系。学妹 提交于 2020-01-24 21:01:30

问题


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();
});

回答1:


setTimeout is exactly what you need.

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



回答2:


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.




回答3:


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");
});


来源:https://stackoverflow.com/questions/2966611/how-to-delay-hiding-of-a-menu-with-jquery-dropdown-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!