Coder Social home page Coder Social logo

Comments (9)

markimark avatar markimark commented on July 28, 2024

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.

from responsive-nav.js.

hvietphan avatar hvietphan commented on July 28, 2024

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

from responsive-nav.js.

andalusi avatar andalusi commented on July 28, 2024

Works great, thanks!

from responsive-nav.js.

angeliquejw avatar angeliquejw commented on July 28, 2024

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?

from responsive-nav.js.

arielsalminen avatar arielsalminen commented on July 28, 2024

@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();
  });
});

from responsive-nav.js.

dibba avatar dibba commented on July 28, 2024

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.

from responsive-nav.js.

disenolopez avatar disenolopez commented on July 28, 2024

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!

from responsive-nav.js.

TheBoyMoe avatar TheBoyMoe commented on July 28, 2024

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!

from responsive-nav.js.

trinzia avatar trinzia commented on July 28, 2024

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();
    }
});

from responsive-nav.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.