代码之家  ›  专栏  ›  技术社区  ›  jake

jQuery悬停菜单-悬停在子菜单上时-菜单消失

  •  2
  • jake  · 技术社区  · 17 年前

    显示/隐藏工作正常,但我不知道如何设置它,以便在鼠标位于div上方时不会隐藏。我尝试使用(此)和.hover,但没有成功。

    这是我的密码:

    $(document).ready(function()
    {
    
        // hide all dropdown
        $("#dropdown1").hide();
    
        //hover show dropdown
    
        $(".menu-level-one").hover(
            function () {
                $("#dropdown1").show();
            },
            function () {
                var postTimer1 = setTimeout(function(){ $("#dropdown1").hide(); }, 1000);        
            }
        );
    });
    
    1 回复  |  直到 16 年前
        1
  •  2
  •   Zach Langley    17 年前

    你可以用 clearTimeout(postTimer1) 停止计时器的执行。因此,如果用户将鼠标悬停在 #dropdown1

    也许是这样的:

    $(document).ready(function() {
      var hideTimer = null
      var dropdown = $("#dropdown1", this)
      var menu = $(".menu-level-one", this)
    
      dropdown.hide();
    
      $([dropdown[0], menu[0]]).hover(
        function() {
          if (hideDropdownTimer)
            clearTimeout(hideDropdownTimer);
    
          dropdown.show();
        },
        function() {
          if (hideDropdownTimer)
            clearTimeout(hideDropdownTimer);
    
          hideDropdownTimer = setTimeout(function() {
            dropdown.hide()
          }, 300)
        }
      )
    })