Bourbon Refill Navigation Menu slide up on click
Asked Answered
D

4

7

I am using the Bourbon Refill navigation menu, and want to modify it so when a link is clicked on in small mode the menu slides back up. At the moment the menu drops down, but when a menu item is clicked the menu stays dropped down. As I am using scroll-on-page with a fixed top menu this means a lot of the content is hidden behind the menu.

Here is the code on Codepen:

http://codepen.io/mikehdesign/pen/LVjbPv/

My existing code is below:

HTML

<header class="navigation" role="banner">
<div class="navigation-wrapper">
<a href="javascript:void(0)" class="logo">
<img src="https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/placeholder_logo_1_dark.png" alt="Logo Image">
</a>
<a href="javascript:void(0)" class="navigation-menu-button" id="js-mobile-menu">Menu</a>
<nav role="navigation">
  <ul id="js-navigation-menu" class="navigation-menu show">
    <li class="nav-link"><a href="javascript:void(0)">About Us</a></li>
    <li class="nav-link"><a href="javascript:void(0)">Contact</a></li>
    <li class="nav-link"><a href="javascript:void(0)">Testimonials</a></li>
    <li class="nav-link"><a href="javascript:void(0)">Sign up</a></li>
  </ul>
</nav>

SCSS

.navigation {
$large-screen: em(860) !default;
$large-screen: $large-screen;

// Mobile view
.navigation-menu-button {
display: block;
float: right;
margin: 0;
padding-top: 0.5em;

@include media ($large-screen) {
  display: none;
}
}

// Nav menu
.navigation-wrapper {
@include clearfix;
position: relative;
}

.logo {
float: left;

img {
  max-height: 2em;
  padding-right: 1em;
}
}

nav {
float: none;

@include media ($large-screen) {
  float: left;
  line-height: 1.5em;
  padding-top: 0.3em;
}
}

ul.navigation-menu {
clear: both;
display: none;
margin: 0 auto;
overflow: visible;
padding: 0;
width: 100%;

@include media ($large-screen) {
  display: block;
  margin: 0;
  padding: 0;
}

&.show {
  display: block;
}
}

// Nav items
ul li.nav-link {
display: block;
text-align: right;
width: 100%;

@include media ($large-screen) {
  background: transparent;
  display: inline;
  text-decoration: none;
  width: auto;
}
}

li.nav-link a {
display: inline-block;

@include media ($large-screen) {
  padding-right: 1em;
}
}
}

JS

$(document).ready(function() {
var menuToggle = $('#js-mobile-menu').unbind();
$('#js-navigation-menu').removeClass("show");

menuToggle.on('click', function(e) {
e.preventDefault();
$('#js-navigation-menu').slideToggle(function(){
if($('#js-navigation-menu').is(':hidden')) {
$('#js-navigation-menu').removeAttr('style');
  }
});
});
});

Help greatly appreciated

Mike

Danieledaniell answered 13/3, 2015 at 15:16 Comment(1)
This is what you want to do? jsfiddle.net/lmgonzalves/czm9L0snOeillade
D
-1

So I sorted this by detecting whether the menu button was visible, and then using that as the toggle in the JQuery:

$(document).ready(function() {
  var menuToggle = $('#js-mobile-menu').unbind();
  $('#js-navigation-menu').removeClass("show");

  $('li.nav-link').on('click', function(){
    if($(".navigation-menu-button").is(":visible")  )  {
      menuToggle.trigger('click');
 } 
  }); 

  menuToggle.on('click', function(e) {
    e.preventDefault();
    $('#js-navigation-menu').slideToggle(function(){
      if($('#js-navigation-menu').is(':hidden')) {
        $('#js-navigation-menu').removeAttr('style');
      }
    });
  });
});
Danieledaniell answered 22/6, 2015 at 20:57 Comment(1)
Only a javascript beginner, so if someone could let me know why this was downvoted and how it could be improved that would be greatDanieledaniell
C
3

Check out the following demo tested working in small and large version as your expectation.

EDIT 2

Updated toggling for small version as per your requirement.

Codepen

jQuery

function smallVersion() {
  $("#js-navigation-menu a").on('click', function(e) {
    $('#js-navigation-menu').slideToggle(function() {
      if ($('#js-navigation-menu').is(':hidden')) {
        $('#js-navigation-menu').removeAttr('style');
      }
    });
  });
}

function dothis(){
    var ww = $(window).width();
    var emToPx = 53.75 * parseFloat($("html").css("font-size"));
    if( ww < emToPx ) {
        smallVersion();
    }
}

$(document).ready(function() {
  var menuToggle = $('#js-mobile-menu').unbind();
  $('#js-navigation-menu').removeClass("show");

  menuToggle.on('click', function(e) {
    e.preventDefault();
    $('#js-navigation-menu').slideToggle(function(){
      if($('#js-navigation-menu').is(':hidden')) {
        $('#js-navigation-menu').removeAttr('style');
      }
    });
  });

  dothis();
});
Candless answered 15/6, 2015 at 13:13 Comment(4)
That works ok on the small menu, but is slide toggling on the large version as well. Need it to just work on the small dropdown menuDanieledaniell
@MikeHarrison I've Edited, Please check.Candless
I would need it to work with only one nav list rather than two, don't want to duplicate content unnecessarilyDanieledaniell
@MikeHarrison I've updated my code hope it meets your exception.Candless
U
0

You could try...

$('header.navigation').on('click',function(){
    $('header.navigation ul.submenu').css('display','none');
})
Uzzi answered 13/3, 2015 at 15:29 Comment(1)
Sorry that didn't work - I need the drop down to slide up when a li.nav-link is clicked.Danieledaniell
A
0

Check following code, hope it helps.

I have used matchmedia to check screen resolution. IE 10 and above supports it. You can use matchmedia polyfill https://github.com/paulirish/matchMedia.js/ for IE9 and below.

"min-width: 53.75em" is from Refills css after this point it changes to mobile navigation.

$('li.nav-link').on('click', function(){
    if(!window.matchMedia("(min-width: 53.75em)").matches)  {
       menuToggle.trigger('click');
    } 
}) 

http://codepen.io/praveenvijayan/pen/YXrbKB

Attalanta answered 22/6, 2015 at 10:12 Comment(0)
D
-1

So I sorted this by detecting whether the menu button was visible, and then using that as the toggle in the JQuery:

$(document).ready(function() {
  var menuToggle = $('#js-mobile-menu').unbind();
  $('#js-navigation-menu').removeClass("show");

  $('li.nav-link').on('click', function(){
    if($(".navigation-menu-button").is(":visible")  )  {
      menuToggle.trigger('click');
 } 
  }); 

  menuToggle.on('click', function(e) {
    e.preventDefault();
    $('#js-navigation-menu').slideToggle(function(){
      if($('#js-navigation-menu').is(':hidden')) {
        $('#js-navigation-menu').removeAttr('style');
      }
    });
  });
});
Danieledaniell answered 22/6, 2015 at 20:57 Comment(1)
Only a javascript beginner, so if someone could let me know why this was downvoted and how it could be improved that would be greatDanieledaniell

© 2022 - 2024 — McMap. All rights reserved.