0

In the navigation bar, on click of "Home 3" menu, a dropdown menu appears through jQuery. I want to hide this menu when I hover on other links of the menu for which I have included style as below:

.nav-ul .menu-item:hover ul{
    display:none;
}

Here is the fiddle: http://jsfiddle.net/Tw44R/1/

4
  • 1
    I don't think what you're trying to do is possible using pure CSS. Commented Jul 15, 2014 at 12:47
  • It can't be accomplished with CSS in general case. But for your specific markup there is a trick jsfiddle.net/Tw44R/13 Commented Jul 15, 2014 at 12:53
  • @YuryTarabanko What changes did you make in the fiddle ? Commented Jul 15, 2014 at 12:55
  • @user2585299 css line 60 Commented Jul 15, 2014 at 12:57

4 Answers 4

1

The selector .nav-ul .menu-item:hover ul will only target the <ul> inside the .menu-item item being hovered.

You can't traverse up the DOM using CSS as of now.

Add the following script:

$('.menu-item').hover(function(){
    $(this).siblings().find('ul').hide();
});

Demo

Update:

If you want to hide the dropdown when the mouse moves away, you can use the second callback of hover, as given below:

$('.menu-item').hover(function () {
    $(this).siblings().find('ul').hide();
},

function () {
    $(this).find('ul').hide()
});

Demo

Side note: For anyone down voting seeing Yury Tarabanko's comment, it's not a reliable solution for the task at hand (it doesn't work if the submenu is before the hovered item).

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

Comments

1

Try with jQuery's mouseenter:

$(function () {
    $(".nav-header .nav-ul .menu-item .menu-item-link").click(function (link) {
        if (link.currentTarget.text === 'Home 3') {
            $(this).next("ul").css('display', 'block');
        }
        link.stopPropagation();
    }).mouseenter(function () {
        $(".nav-header .nav-ul .menu-item ul:visible").css('display', 'none');
    });
});

Demo

1 Comment

Your solution also hides the dropdown when I hover on Home 3.
1

This solution will open the sub-menu with a click and close the sub-menu when you move away from either the header or the sub-menu.

I gave each header item an id

<li class="menu-item" id="3">

Then:

$(function(){
   $("#3").click(function(){
     $(".nav-ul .menu-item ul").css('display', 'block');
   });
   $("#3").mouseleave(function(){
     $(".nav-ul .menu-item ul").css('display', 'none');
   });
});

Check it out:

http://jsfiddle.net/Tw44R/20/

Comments

1

You can use this approach:

$(".nav-header .nav-ul .menu-item").hover(function (){
        $(".menu-item").each(function(){
            if($(this).find("ul").css("display") == "block")
                $(this).find("ul").css("display","none");
        });
    })

Check out this JSFiddle..

EDIT: I shouldn't used .menu-item-link, just edited the JSFiddle too. Everything works fine.. Thank you for pointing out my mistake..

1 Comment

Your solution hides the dropdown menu when I am hovering over the sub menus.

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.