Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show submenu's submenu on submenu hover only

Tags:

html

jquery

I am trying to show the submenu on hovering the menus. I have succeeded in submenu level 1. But when I go to submenu level 2(ie, submenu of submenu) it is not working. I want to show the level 2 submenu only on hovering the level 1 submenu. Following is the code that I have tried

<ul class="nav">
    <li><a href="#">Home</a>
    </li>
    <li><a href="#">About Us</a>
        <ul class="submenu">
            <li><a href="#">Capabilities</a></li>
            <li><a href="#">Approach</a></li>
        </ul>
    </li>

    <li><a href="#">Careers</a>
        <ul class="submenu">
            <li><a href="#">Working With Us</a></li>
            <li><a href="#">Work Culture</a>
                <ul>
                    <li><a href="#">Benefits</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Resources</a>
    </li>
    <li><a href="#">Contact Us</a>
    </li>
</ul>

Following is the jQuery

$('ul.submenu').hide();
$('ul.nav > li').hover(function () {
if ($(this).find('ul.submenu').length > 0) {
    $(this).find('ul.submenu').stop().slideDown('slow');
}
},function () {
    if ($(this).find('ul.submenu').length > 0) {
        $(this).find('ul.submenu').stop().slideUp('slow');
    }
});

Please find the Fiddle Here : http://jsfiddle.net/Midhun28/RbY83/1/

like image 621
Midhun Mathew Avatar asked Apr 06 '26 07:04

Midhun Mathew


2 Answers

as far as i understood you want to slidedown the submenu on hovering the list item one level above

<ul class="submenu">
    <li><a href="#">Working With Us</a></li>
    <li><a href="#">Work Culture</a>
        <ul class="submenu">
            <li><a href="#">Benefits</a></li>
        </ul>
    </li>
</ul>

i added the submenu class to all elements i want them to be initially hidden. then i made some modifications on the jquery code to work with all submenus

$('ul.submenu').hide();
$('ul.nav > li, ul.submenu > li').hover(function () {
if ($('> ul.submenu',this).length > 0) {
    $('> ul.submenu',this).stop().slideDown('slow');
}
},function () {
    if ($('> ul.submenu',this).length > 0) {
        $('> ul.submenu',this).stop().slideUp('slow');
    }
});

hope this will work for you see http://jsfiddle.net/U7mqM/

like image 103
Tschitsch Avatar answered Apr 08 '26 21:04

Tschitsch


Use mouseenter and mouseleave events if you want them to be triggered when you hover on the sub-elements.

$('.nav ul').hide();

$('.nav li').mouseenter(function() {
    $(this).children('ul').stop().slideDown('slow')
}).mouseleave(function() {
    $(this).children('ul').stop().slideUp('slow')
});
like image 36
Artem Fitiskin Avatar answered Apr 08 '26 22:04

Artem Fitiskin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!