0

I'm using Foundation v6.2.0 in my project. I am having some difficulties with getting the top bar to work as desired on small screens. On small screens I want the top level of the navigation to act as a direct link (rather than a trigger to show the sub-menu) and instead move that functionality to the caret (little down arrow) on the right of the top level item, so it now triggers the display of the sub-navigation.

$(document).foundation();
<link href="https://cdn.jsdelivr.net/foundation/6.2.0/foundation.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/foundation/6.2.0/foundation.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav class="title-bar" data-responsive-toggle="top-bar-menu" data-hide-for="medium">
    <div class="title-bar-left">
  <a href="#" class="toggle-topbar menu-icon" data-toggle><span class="hamb-icon"></span></a>
    </div>
 <div class="title-bar-right hide-for-small">  
 </div>
</nav>

<nav class="top-bar main-nav" id="top-bar-menu">
    <div class="row main-nav">
        <div class="top-bar-left">
              <ul class="vertical medium-horizontal menu menu-items" data-closing-time="0" data-responsive-menu="accordion medium-dropdown">
                  <li><a href="http://foundation.zurb.com/">Item 1</a></li>
                  <li><a href="http://foundation.zurb.com/">Item 2</a>
                      <ul class="menu vertical">
                          <li><a href="http://foundation.zurb.com/">Item 2A</a></li>
                          <li><a href="http://foundation.zurb.com/">Item 2B</a></li>
                      </ul>
                  </li>
                  <li><a href="http://foundation.zurb.com/">Item 3</a></li>
                  <li><a href="http://foundation.zurb.com/">Item 4</a></li>
                  <li><a href="http://foundation.zurb.com/">Item 5</a></li>
                  <li><a href="http://foundation.zurb.com/">Item 6</a></li
               </ul>
       </div>  
       <div class="top-bar-right search-bar">
       </div>                      
    </div>
</nav>
 
tymothytym
  • 1,571
  • 3
  • 16
  • 25
webIra7
  • 49
  • 6

1 Answers1

1

If you want (say) Item 2 to act as an <a href="go_here"> link when clicked (rather than a trigger to display Item 2A and Item 2B); I don't think there is a standard way to do this in Foundation. However you can get round it with additional links that are hidden for larger screens or JavaScript/jQuery + CSS + HTML changes:

Additional links

This option doesn't leave you needing to add hacks or additional CSS to hide unwanted menu furniture or override the way foundation menus work. What this does is add a new link that is only visible on small screens whilst leaving the original link untouched to continue acting as a trigger.

<li><a href="http://foundation.zurb.com/">Item 2</a>
    <ul>
         <li class="show-for-small-only">
             <a href="http://foundation.zurb.com/">Item 2 "home"</a>
         </li>
         <li><a href="http://foundation.zurb.com/">Item 2A</a></li>
         <li><a href="http://foundation.zurb.com/">Item 2B</a></li>
    </ul>
</li>

JSfiddle: https://jsfiddle.net/sons9wzr/2/

This is more usable but requires an additional click to expose the sub menu. It would also be possible to do this with JavaScript/jQuery dynamically.

JavaScript/jQuery + CSS + HTML

If the aim is to only have the relevant links for mobile and an expanded menu for desktop, you can hide the sub navigation using a similar added class as in the first example above, add the JavaScript and then hide the redundant carets with CSS.

So your HTML becomes something like:

<li><a class="forced" href="http://foundation.zurb.com/">Item 2</a>
    <ul class="show-for-large-only">
         <li><a href="http://foundation.zurb.com/">Item 2A</a></li>
         <li><a href="http://foundation.zurb.com/">Item 2B</a></li>
    </ul>
</li>

Then use JavaScript (jQuery as it is required by Foundation anyway) to force the click on certain nav items (by adding class="forced" to each <a> you want to force to go to a new location in the HTML above).

An example of JavaScript for small screens only:

//check for screen size = small
if(Foundation.utils.is_small_only()) {
    $('nav').on('click','a.forced', function (e) {
        e.preventDefault;
        window.location.href = $(this).attr('href');
    });
}

(More information on the Media Query JS utility)

This will still leave the caret on the menu item though which may be confusing for users so you can remove it (from ALL sub-nav items) with this:

.is-accordion-submenu-parent > a::after {
    display: none;
}

This should give you the desired effect, though it may require additional maintenance when you update Foundation.

Example in Codepen (without code for small-screens only): http://codepen.io/tymothytym/pen/eZGEER

Edit: OK, so based on this comment:

I want to be able to expand the menu in the mobile view by clicking on the icon at the right of the parent element as well as to be able to click on the parents element itself.

I have created a new solution. This is very much a hack, and not a wonderful solution. I would recommend either finding an alternative navigation set up or working within the framework that Foundation offers.

New HTML (forced class moved to <li> & <span> added)

<li class="forced">
    <span data-item="Item 2" class="show-for-small-only"></span>
    <a href="http://foundation.zurb.com/">Item 2</a>
    <ul class="menu vertical">
        <li><a href="http://foundation.zurb.com/">Item 2A</a></li>
        <li><a href="http://foundation.zurb.com/">Item 2B</a></li>
    </ul>
</li>

New CSS This splits up the contents of the <li> and positions the new sub-element

.is-accordion-submenu-parent > a::after {
    display: none;
}
@media only screen and (max-width: 40em) {
  li.forced span {
    content: 'Item 2';
    display: inline-block;
    padding: .7rem 1rem;
    line-height: 1;
    width: 70%;
    color: #2199e8;
    cursor: pointer;
  }
  .menu > li.forced > a {
      padding: .7rem 1rem;
      line-height: 1;
      float: right;
  }
}

New JavaScript

This changes the text of the link (you'd need to do this for small screens only as shown above... above) makes the new <span> a link, inserts text into it by way of a data attribute & changes the original link text to a caret (to preserve as much original functionality as possible).

$(document).foundation();
$('nav').on('click','.forced span', function () {
  window.location.href = $(this).siblings('a').attr('href');
});
$('.forced > a').html("&#9660;");
$('.forced > span').text($('.forced > span').data("item"));

Example here: http://codepen.io/tymothytym/pen/GZOqvO

I think this is likely to have usability issues and maintenance issues so use with caution.

tymothytym
  • 1,571
  • 3
  • 16
  • 25
  • Thanks for taking time to respond to my post, but I don't seem to get it right. Can you please show me in js fiddle how to integrate it the right way. When I did it myself it, child links in the responsive menu as well as in the desktop view disappeared . Just the icon in the left hand size is changing on click on mobile, but that is it. What am I doing wrong? – webIra7 Apr 01 '16 at 18:51
  • I've updated the answer. I think it might be jsfiddle stopping the link opening as when you take away the javascript entirely, the links still don't resolve. I'll check Codepen... – tymothytym Apr 01 '16 at 19:10
  • Yep seems like a jsfiddle only thing, I've changed the link to Codepen as it works better in this instance. – tymothytym Apr 01 '16 at 19:18
  • Thanks for doing that, but it still not working. I want to be able to expand the menu in the mobile view by clicking on the icon at the right of the parent element as well as to be able to click on the parents element itself. In your prototype second level nav is not available. – webIra7 Apr 04 '16 at 18:28
  • That's a bit of a fundamental change in how the nav works. To do this you'd need a good deal more hacking of the foundation code to the point where you might as well use a different navigation from the one bundled with foundation. You would need to split the relevant `
  • ` in half, possibly by adding an element, then override the default behaviour, then override the default styles. If you really, REALLY, want to do this why don't you update your code & question with any progress you have made and your exact requirements?
  • – tymothytym Apr 04 '16 at 19:10