Jake
Jake

Reputation:

jQuery Hover Menu - When hovering over child - menu disappears

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

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.

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

Upvotes: 2

Views: 4551

Answers (1)

Zach Langley
Zach Langley

Reputation: 6786

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

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

Upvotes: 2

Related Questions