Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close the Foundation topbar menu when a link is clicked?

I am using Zurb Foundation's fixed topbar on my single page site, and it contains anchor links to places within the page. I would like it so that whenever a link inside the expanded mobile menu is clicked, the menu closes.

Currently, when a link is clicked, the page scrolls, but the menu is left open at the top of the page out of view.

In a previous version of Foundation, I was able to reverse engineer the code and find a simple solution. Since updating to 4.3.1 to fix another issue with the topbar, I am unable to find a solution due to my limited knowledge of javascript.

topbar

It seems to me that if I could fire the event or function for menu closing when a link in the menu is clicked, it would be fixed. Previously, I put my code that happened on a link click on line 261.

When the mobile menu closes, .fixed is added to the div surrounding the topbar, while .expanded and .fixed are removed from the .topbar div.

like image 794
Wookieguy Avatar asked Aug 29 '13 16:08

Wookieguy


3 Answers

You can try adding some jQuery to collapse the menu when you click on a link.

You can add the code wrapped inside a script element. Place it after all your html elements (inside body element). You can also put it in a separate javascript file that you can source like you would any other javascript file. Make sure you put that link after jquery link.

The code itself can be pretty simple since it looks like foundation adds a class called "expanded" to the nav bar when you toggle the menu icon on and off. So you can just remove the "expanded" class when someone clicks on your buttons.

This is what it should look like:

jQuery(document).ready(function($) {
    $('.top-bar ul.right(or .left depending how you arranged your buttons) li').click(function() {
       $('.top-bar').removeClass('expanded');
    });
}(jQuery));

The selector '.top-bar ul.right(or .left depending how you arranged your buttons) li' can be called with an id also if you give your menu (the ul element) a unique id. In this case it would be:

jQuery(document).ready(function($) {
    $('#myMenuId li').click(function() { 
        $('.top-bar').removeClass('expanded'); 
    });
}(jQuery));

Hope that helps.

like image 113
pr0tipZ Avatar answered Nov 14 '22 14:11

pr0tipZ


Try:

$('#main-menu li').click(function() {
    $('.toggle-topbar').trigger('click');
});
like image 3
Tyler Smith Avatar answered Nov 14 '22 14:11

Tyler Smith


I'm working with Foundation 6 for the first time, and I ran across this post while trying to figure out a way to close the new top-bar menu on mobile when a link had been clicked. I wanted to comment with my solution in case anyone else working on Foundation 6 runs across this post, since it was a good starting place for me.

Here's what I did:

Navigation setup - horizontal nav on medium and large breakpoints, responsive toggle vertical nav on small breakpoint

                <!--  Mobile responsive toggle (hamburger menu) -->
                <div class="title-bar" data-responsive-toggle="siteNav" data-hide-for="medium">
                    <button class="menu-icon" type="button" data-toggle></button>
                    <div class="title-bar-title">Menu</div>
                </div>

                <!-- Nav items -->
                <div id="siteNav" class="top-bar"> 
                    <p><ul class="vertical medium-horizontal menu text-center">
                        <li ><a href="#home" onClick="">HOME</a></li>
                        <li ><a href="#services">SERVICES</a></li>
                        <li ><a href="#contact">CONTACT</a></li>
                    </ul></p>
                </div>

Then I added a modified version of the jquery based on the previous solutions in this post (thanks to amazingBastard and Cerbrus):

$(document).ready(function($) {
        $('#siteNav li').click(function() { 
            if(Foundation.MediaQuery.current == 'small'){
                $('#siteNav').css('display', 'none'); 
            }
        });
    });

In Foundation 6 the css selector "display" is added to an expanded menu and set to either "display:none" for hidden or "display:block" for expanded. This jquery snippet checks the current breakpoint against small(mobile device) on click of a nav item in the default menu class I am using, and if true changes the css selector to "display:none", effectively closing the menu toggle.

like image 2
Jeremy Simons Avatar answered Nov 14 '22 12:11

Jeremy Simons