1

I have a problem when I'm using .click() and .hover() in combination with toggle.

I have a "menu" which should be opened/closed on hover, but can also be closed/reopened on click. But if I'm opening the menu with hover and then close it with a click, it opens again If i stop hovering it.

Here's my code (jsfiddle)

HTML

<ul>
<li><span>Click Me</span>
    <ul>
        <li><a href="#">Option 1</a></li>
        <li><a href="#">Option 1</a></li>
        <li><a href="#">Option 1</a></li>
    </ul>
</li>
</ul>

JS

$(function() {
    $("ul").hover(function() {
        $(this).find('ul').toggle();
    });
    $("ul").click(function() {
       $(this).find('ul').toggle();
    });
});

CSS

ul{
    margin: 0;
    padding: 0;
    border: 1px solid black;
    position: relative;
}

li{
    list-style-type: none;
}

ul ul{
    display: none;
    position: absolute;
    width: 100%;
    left: -1px;
}

span{
    display: block;
    padding: 5px;
    background-color: #eeeeee;
}

a{
    background-color: #eee;
    display: block;
    padding: 5px;
}

a:hover{
    background-color: white;
}

I know what the problem is, but I don't know how to fix it. I hope someone has a solution.

5 Answers 5

2

You have to write two hover function for mouse in and out instead of using toggle like below :

$(function() {
        $("ul").hover(function() {
            $(this).find('ul').show();
        },function() {
            $(this).find('ul').hide();
        });
        $("ul").click(function() {
           $(this).find('ul').toggle();
        });
    });

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, that works great! Thanks :) Do you know how I can change the behaviour on a tablet that I need a "double tap" because of the hover effect.
you mean you want to use double click instead of click?
Yep, I don't like it that a tablet user has to click 2 times.
2

Use the first hover constructor:

$("ul").hover( handlerIn, handlerOut )

...instead of second which has same function for both operations:

.hover( handlerInOut )

Regards ;)

Comments

2
$(function() {
   $("ul").hover(function() {
     $(this).find('ul').show();
   }, 
   function(){
     $(this).find('ul').hide()
   });
   $("ul").click(function() {
     $(this).find('ul').toggle();
   });
});

Add mouseout function also in your hover function.

Demo:

http://jsfiddle.net/6jzdgcap/

Comments

2

You may also prefer to use mouseenter (http://api.jquery.com/mouseenter/) and mouseleave (http://api.jquery.com/mouseleave/) if you find this easier to read.

    $(function() {
        $("ul")
        .mouseenter(function() {
            $(this).find('ul').show();
        })
        .mouseleave(function() { 
           $(this).find('ul').hide(); 
        });

        $("ul").click(function() {
           $(this).find('ul').toggle();
        });
    });

Comments

1

Instead of toggle() which creates the issue, You can use slideUp() and slideDown() separately in the callbacks functions of hover as follows:

$(function () {
     $("ul").hover(function () {
            $(this).find('ul').slideDown();
     },
     function () {
            $(this).find('ul').slideUp();
     });

     $("ul").click(function () {
            $(this).find('ul').toggle();
     });
});

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.