jQuery悬停菜单 - 当鼠标悬停在菜单上时 - 菜单消失

时间:2020-12-29 20:33:29

So I created a simple little hover, that uses a class from a link to show a div underneath.

所以我创建了一个简单的小悬停,它使用链接中的一个类来显示下面的div。

The show/hide works fine, but I can't figure out how to set it so that if the mouse is over the div, it wont hide. I tried using (this) and .hover, with no success.

显示/隐藏工作正常,但我无法弄清楚如何设置它,以便如果鼠标在div上,它不会隐藏。我尝试使用(this)和.hover,但没有成功。

Here is my code:

这是我的代码:

$(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 个解决方案

#1


2  

You can use clearTimeout(postTimer1) to stop the timer from executing. So if the user hovers over #dropdown1, clear the timer.

您可以使用clearTimeout(postTimer1)来停止执行计时器。因此,如果用户将鼠标悬停在#dropdown1上,请清除计时器。

Maybe something like this:

也许是这样的:

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

#1


2  

You can use clearTimeout(postTimer1) to stop the timer from executing. So if the user hovers over #dropdown1, clear the timer.

您可以使用clearTimeout(postTimer1)来停止执行计时器。因此,如果用户将鼠标悬停在#dropdown1上,请清除计时器。

Maybe something like this:

也许是这样的:

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