How can I show a div only when hovering a menu item or the div?

后端 未结 2 1388
小鲜肉
小鲜肉 2021-01-07 15:10

I have items contained in an unordered list. I have hidden divs (using display:none;) outside of this list. When you hover over a list item, I would like the div to appear

2条回答
  •  忘掉有多难
    2021-01-07 15:42

    Something like this should work.

    Using .hover to bind the event, .index and .eq to find which one to show/hide and the native JS methods for timeouts to let you hover the divs.

    var timeout; // store a timeout here
    
    $('#main-menu lia ').hover(function ()
    {
        $('.widget-container').eq($(this).parent().index()).show();
    }, function ()
    {
        timeout = setTimeout(function ()
        {
            $('.widget-container').eq($(this).parent().index()).hide();
        }, 1000);
    );
    
    $('.widget-container').hover(function ()
    {
        clearTimeout(timeout);
    }, function ()
    {
        timeout = setTimeout(function ()
        {
            $(this).hide();
        }, 1000);
    });
    

提交回复
热议问题