1

I'm making a navigation menu for my website. I wanted it to look like Facebook's top navigation. The menu expands only when it's clicked and disappears when you click somewhere else.

I seem have found a way to expand the list by clicking the #menu, but it won't disappear after that. I have no idea how to do so. I'm new to jQuery. I've tried to use and implement the 'if else' jQuery function, but still have no result.

Here's the jQuery listing code:

$(function() {
    // OPACITY OF BUTTON SET TO 50% and DISPLAY LIST AS NONE
    $("#menu").css("opacity","0.5");
    $(".list").css("display","none");

    // ON MOUSE CLICK
    $("#menu").click(function () {
        // SET OPACITY TO 100% and DISPLAY LIST
        $(this).stop().animate({ opacity: 1.0 }, "slow");
        $(".list").css("display","inline");
    });
});

and the HTML is simply:

<div id="menu">
    <a href="where.com"><span class="menu">MENU</span></a>
</div>

Thanks for helping... regards

3 Answers 3

1

Try using jquery's one() method to bind the document to cause the menu to disappear.

$(function() {
    // OPACITY OF BUTTON SET TO 50% and DISPLAY LIST AS NONE
    $("#menu").css("opacity", "0.5");
    $(".list").hide();

    // ON MOUSE CLICK
    $("#menu").click(function(event) {
        event.preventDefault();
        event.stopImmediatePropagation();

        // SET OPACITY TO 100% and DISPLAY LIST
        $(this).stop().animate({
            opacity : 1.0
        }, "slow");
        $(".list").css("display", "inline");


        // this will bind the entire document for a single click
        $(document).one("click", function() {
            // do something to hide the menu here...
            $("#menu .list").hide();

        });

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

4 Comments

I'm trying this out, and it seems the handler for .one(... gets invoked immediately after clicking the menu (possibly because it is in the middle of processing another click?) - jsfiddle.net/PMdVS/4
$("#menu").click(function(event) { plus event.stopPropagation(); will work. See: jsfiddle.net/PMdVS/7
entirely possible...in my code I always do a event.preventDefault() & an event.stopImmediatePropagation()...I'll add that to the answer.
thank so much you both.. It now works perfectly like this: jsfiddle.net/2THRL/7 :)
0

You can try something like this:

$('html').click(function() {
 $(".list").css("display","none");
});

$('#menu').click(function(event){
 event.stopPropagation();
});

it should work :)

1 Comment

The jquery methods .show() and .hide() or even .toggle() are preferred preferred over setting the css "display". This will also cause ANY element with a class="list" to be hidden.
0

Edit:

I found something that matches your request a little bit better (though still not exactly). Try the .mouseleave() function to handle the event where you leave focus of the menu.

I'd suggest .focusout(), but it seems the focus would have to be set to the menu somehow, and I can't seem to get that to work.

http://jsfiddle.net/PMdVS/2/

$(function()
{
    // OPACITY OF BUTTON SET TO 50% and DISPLAY LIST AS NONE
    $("#menu").css("opacity","0.5");
    $(".list").css("display","none");

    // ON MOUSE CLICK
    $("#menu").click(
        function() {
            $(this).stop().animate({opacity: 1.0}, "slow");
            $(".list").css("display","inline");
        }
    );
    // ON MOUSE LEAVE
    $("#menu").mouseleave(
        function() {
            $(this).stop().animate({opacity: 0.5}, "slow");
            $(".list").css("display","none");
        }
    );
});

The menu expands only when it's clicked and disappears when you click somewhere else.

Not completely sure if you wanted the menu to be an on-click thing, or if you just used that because that's what you found.

Try the .hover() method, which takes a handler for when you start hovering, and when you stop hovering:

http://jsfiddle.net/PMdVS/

$(function()
{
    // OPACITY OF BUTTON SET TO 50% and DISPLAY LIST AS NONE
    $("#menu").css("opacity","0.5");
    $(".list").css("display","none");

    // ON MOUSE OVER
    $("#menu").hover(
        function() {
            // SET OPACITY TO 100%
            $(this).stop().animate({opacity: 1.0}, "slow");
            $(".list").css("display","inline");
        },
        function() {
            $(this).stop().animate({opacity: 0.5}, "slow");
            $(".list").css("display","none");
        }
    );
});

7 Comments

The OP asked for the click to show and hide the menu this is a lot of information about hovering which would be great if the question were about how to make a menu appear/hide upon hover.
@kasdega: Yes, you're correct. I was under the assumption that it wouldn't be possible, though you've proven otherwise.
:) great answer though for the next guy looking to show/hide via hover
you could also check out the fantastic jquery plugin hoverIntent
hi Merlyn... thanks for the answer I've tried it. And it works cool.. edit: jsfiddle.net/2THRL/2 I wish the menu to disappear when I click somewhere else in the body. Not with the .mouseleave
|

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.