Skip to content Skip to sidebar Skip to footer

Hide Menu Onclick

I have this dropdown menu, that will slideDown whenever my users click on a link. However, I don't know how to make it slideUp, whenever the user click somewhere on the page (Not o

Solution 1:

Slide up all visible menus when a click propagates to the document:

$(document).on("click", function(){
    $(".subnav:visible").slideUp();
});

Then prevent this from happening when you're within menus by stopping the event propagation.

$("ul.topnav").on("click", "li", function(e){
    e.stopPropagation();
    $(".subnav:visible", $(this).siblings()).slideUp("fast");
    $(".subnav", this).slideDown();
});

Fiddle: http://jsfiddle.net/jonathansampson/qQsdN/

Solution 2:

This JQuery should work:

$('#nav li').hover(
    function () {
        //show its submenu
        $('ul', this).slideDown(100);

    },
    function () {
        //hide its submenu
        $('ul', this).slideUp(100);        
    }
);

Solution 3:

jQuery(this).find() will find descendents of "this." In your code, "this" has changed because of the new closure "function()."

Try this:

jQuery("ul.topnav li").click(function() { //When trigger is clicked...  var me = this;
    //Following events are applied to the subnav itself (moving subnav up and down)  jQuery(me).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  jQuery(me).click(function() {  
    }, function(){  
        jQuery(me).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
    });  

});

Post a Comment for "Hide Menu Onclick"