1

I am trying to make the smallest menu with submenus possible. So far, here is my code:

<div id="ctl100_cphHeader_NavigationBar">
    <li></li> <!-- makes first left-border in css -->
    <li><a class="navlink" href="/ComingSoon.aspx">N/A</a></li>
    <li><a class="navlink" href="/ComingSoon.aspx">N/A</a></li>
    <li>
        <a class="navlink" href="/ComingSoon.aspx">N/A</a>
        <div class="submenu">
            <a class="navlink" href="/">Home</a>
            <a class="navlink" href="/">Home</a>
        </div>
    </li>
    <li>
        <a class="navlink" href="/ComingSoon.aspx">N/A</a>
        <div class="submenu">
            <a class="navlink" href="/ComingSoon.aspx">Coming Soon</a>
        </div>
    </li>
</div>

I am wanting to use jquery to make it so on $('.navlink').mouseenter() any submenus under it will appear, and on $('.navlink').mouseleave() any submenus under it will disappear. How would I go about this? I am new to jquery and not good with selectors.

3 Answers 3

2

Try the following script:

$(document).ready(function () { 
    var speed = 500;//The speed is in milliseconds 
    $('li').hover(function () {
         //show its submenu
         $(this).children('.submenu').stop().slideDown(speed);
        },
        function () {
         //hide its submenu
         $(this).children('.submenu').stop().hide(speed);      
     });
});​

I just used the hover function, the first function(){... is the onMouseEnter and the second function(){... is the onMouseLeave. Also remember that the submenu's need to bedisplay:none.

You can do this with javascript by adding the following code to the top of the previous one:

$('.submenu').hide();

I recommend using the CSS to hide them though:

.submenu{
  display:none
}

You don't need to add that extra function in the JavaScript.

Check out the JSfiddle so see this code in action!

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

1 Comment

Thanks for the brief and simple answer. :)
0

Working Demo http://jsfiddle.net/QYEWs/

Since you have not mentioned anything about Jquery code you are using use the code mentiond in jsfiddle.

The working demo should help.

This is good place to start with Jquery http://jquery.com/

Code

// Hide inactive ones
$('#main-nav > li:not(.current-menu-parent) .sub-menu').hide();

// What to do when hovering over a menu item
$("#main-nav > li").hoverIntent({
    over: function(){
        // Find the hovered menu's sub-menu
        var $submenu = $(this).find('.sub-menu');

        // Stop the animation and fade out the other submenus
        $('.sub-menu').not($submenu).stop(true,true).fadeOut(260);

        // Fade in the current one
        $submenu.fadeIn(260);
    }
});

// What to do when user leaves the navigation area
$('#main-nav').mouseleave(function(){
    // Fade out all inactive submenus
    $('#main-nav > li:not(.current-menu-parent) .sub-menu').fadeOut(260);

    // Fade in the active one
    $('.current-menu-parent .sub-menu').fadeIn(260);
});​

2 Comments

@Shawn31313 cool, I am guessing that OP is using .net user controllers and might need css etc with the menu, anyhoo cheers for comment :)
Yup, also why use hoverIntent?
-1

To get it to show (Assuming you already have the submenu hidden) you could do something like this

$('.navlink').hover(function(){
   $(this).siblings('submenu').show();
})

The catch is that you don't want to hide it when leaving the navlink, because you have to leave the navlink to get to the submenu. So you likely have to attach the hiding part to the parent LI.

Also you'll need to consider devices that aren't using mice...such as keyboards and touch devices.

4 Comments

Your not leaving the navlink to get to the submenu since the submenu is inside of the navlink.
@Shawn31313 look again. You are incorrect. .navlink is a sibling of .submenu--not a parent. Not sure why this got a downvote (whoever did it, please explain)
Mmm, I see, well your code is still not really close to what he needed. Instead of doing the hover function on .navlink all you would of had to do is do it on the li. Then find the submenu child.
@Shawn31313 Yep. I could have done that. In fact, if you read my answer, I say exactly that. But that wasn't the question, though. Sometimes I try to help the OP think bit. To each their own.

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.