Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close on click #39

Closed
martinconnelly opened this issue May 20, 2013 · 9 comments
Closed

Close on click #39

martinconnelly opened this issue May 20, 2013 · 9 comments

Comments

@martinconnelly
Copy link

First: Thanks. This is awesome.

I'm hoping there's a good and easy way to get the menu to toggle closed when you click li item. My guess is that that's what the public methods are for, but, that's a guess, and as far as it goes.

Many thanks again,
-m

@markimark
Copy link

I am also looking for exactly that. It does not feel natural to me if the menu stays open after a link was clicked. Is there any way to get done what @martinconnelly was asking for?

Note: Request is for one page web design. On a design like that, the menu always stays open after a link was clicked.

@hvietphan
Copy link

Just bump into this today and was struggling with that for a bit too. Not sure if you guys already got the solution, but for anyone who is interested. I would go at it with this short addition to the end of the script. All it does is triggering the toggle when any links in the menu is clicked.

Basically, just add the function below

$("#nav a").click(function(){navigation.toggle();});

within the Open callback option and it works like a charm

@andalusi
Copy link

Works great, thanks!

@angeliquejw
Copy link

I'm looking for exactly this solution, but, unfortunately, hvietphan's function isn't working for me. I've tried adding it in my in-page script as well as the original JS file, to no avail. Has anything changed in the last 3 mos that would have made this not work and, if so, is there a new solution?

@arielsalminen
Copy link
Owner

@angeliquejw Might be because @hvietphan's solution requires jQuery to work? without jQuery that would same code would be:

[].forEach.call(document.querySelectorAll("#nav a"), function(el) {
  el.addEventListener("click", function() {
   navigation.toggle();
  });
});

@dibba
Copy link

dibba commented Sep 30, 2013

I couldn't get hvietphan's solution to work either unfortunately (with jQuery running).

There's no navigation object as far as I can see so I'm not sure how to run the toggle method.

@disenolopez
Copy link

Hi, thank you so much for this great script. I'm also trying to achieve closing the navigation upon clicking, or upon reaching the section that is linked. Any help you can provide would be appreciated. Thanks!

@TheBoyMoe
Copy link

I was having the same issue, using the script on a one page site. I just edited the script provided by @hvietphan.

var nav = responsiveNav('.nav-collapse', {
open: function(){
$('.nav-collapse a').click(function(){
nav.toggle();
});
}
});

Works a treat!

@trinzia
Copy link

trinzia commented Nov 5, 2013

I had some false starts making the menu close only in mobile view, so here's working code for future vistors to this thread. In your click function:

if ($(".nav-toggle").css("display") == "block" ){
        nav.toggle();
}

Note that, rather than using screen width, I'm checking for a condition which is controlled by the media query. This is to avoid inconsistencies caused by whether or not the scrollbar is included.
Reference: http://www.fourfront.us/blog/jquery-window-width-and-media-queries

More detailed example:

$('#myselector a').click(function (e) {
    if ($(".nav-toggle").css("display") == "block" ){
        nav.toggle();
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants