0

I created a simple hamburger menu for my site, I just can not get it to close after clicking on a link and going to the anchor that I put in and that also closes when I click off the menu.

My HTML is:

  $(document).ready(function() {
    $('.navbar-fostrap').click(function() {
      $('.nav-fostrap').toggleClass('visible');
      $('body').toggleClass('cover-bg');
    });
  });
nav {
  height: 60px;
  text-align: center;
  position: relative;
  width: width:100%;
  margin: auto;
  z-index: 100000;
}

.navbar-default {
  transition: 500ms ease;
  background: transparent;
}

.navbar-default.scrolled {
  background: rgba(28, 28, 28, 0.8);
}

a.title {
  float: left;
  font-size: 25px;
  color: white;
  font-family: quantum;
  height: 55px;
  padding-top: 2px;
}

.title {
  float: left;
  display: inline-block;
  padding: 0;
  margin-top: 5px;
  list-style: none;
  height: 55px;
}

.nav-fostrap ul {
  list-style-type: none;
  margin-top: 5px;
  padding: 0;
  display: block;
  float: right;
  width: auto;
}

.nav-fostrap ul li {
  display: inline-block;
  font-weight: bold;
  text-transform: uppercase;
  text-align: center;
  padding-left: 15px;
  padding-right: 15px;
  margin-top: 0;
  list-style: none;
  height: 60px;
}

a.menu {
  font-family: Montserrat, Arial;
  font-weight: 700;
  position: relative;
  display: inline-block;
  cursor: pointer;
  padding: 8px 0;
  margin-top: 14.5px;
  margin-bottom: 10.5px;
  color: #fff;
  text-decoration: none;
  height: 40px;
  top: -10px;
}

a:hover.menu {
  color: #fff;
  text-decoration: none;
}

.menu a:before,
a:after {
  content: '';
  position: absolute;
  width: 0%;
  height: 2px;
  bottom: -2px;
  background: #fff;
}

.menu a:before {
  left: 0;
}

.menu a:after {
  right: 0;
  background: #fff;
  transition: width 0.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:before {
  background: #fff;
  width: 100%;
  transition: width 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:after {
  background: transparent;
  width: 100%;
  transition: 0s;
}

.title-on-mobile {
  display: none;
}

@media only screen and (max-width:768px) {
  a.title {
    float: none;
    font-size: 25px;
    color: white;
    font-family: quantum;
    height: 55px;
    padding-top: 2px;
    width: 100%;
    text-align: center;
    position: fixed;
    left: -50px;
  }
  .nav-fostrap {
    background: #1c1c1c;
    width: 200px;
    height: 100%;
    display: block;
    position: fixed;
    left: -200px;
    top: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    margin: 0;
    border: 0;
    border-radius: 0;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
    z-index: 105;
  }
  .nav-fostrap.visible {
    left: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    z-index: 90;
  }
  .nav-bg-fostrap {
    display: inline-block;
    vertical-align: middle;
    width: 100%;
    height: 60px;
    margin: 0;
    position: absolute;
    top: 0px;
    left: 0px;
    background: transperant;
    padding: 12px 0 0 10px;
    -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -ms-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -o-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    z-index: 100;
  }
  .navbar-fostrap {
    display: inline-block;
    vertical-align: middle;
    height: 50px;
    cursor: pointer;
    margin-top: 5px;
    position: absolute;
    top: 0;
    left: 0;
    padding: 12px;
    z-index: 102;
  }
  .navbar-fostrap span {
    height: 2px;
    background: #fff;
    margin: 5px;
    display: block;
    width: 20px;
  }
  .navbar-fostrap span:nth-child(2) {
    width: 20px;
  }
  .navbar-fostrap span:nth-child(3) {
    width: 20px;
  }
  .nav-fostrap ul {
    padding-top: 50px;
  }
  .nav-fostrap li {
    display: block;
  }
  .nav-fostrap li a {
    display: block;
    color: #505050;
    font-weight: 600;
  }
  .nav-fostrap li:first-child:hover a {
    border-radius: 0;
  }
  .cover-bg {
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}
<nav class="menu navbar navbar-default navbar-fixed-top">
  <div class="headercontainer">
    <a class="title" href="#seccao-inicio"><img src="img/logo.png" alt="logo" width="80" href="#" style="position: relative;margin-left: 12px; float:right; top:2.5px;position: relative;" /></a>

    <div class='nav-fostrap'>
      <ul class="menu">
        <li><a class="menu" href="#seccao-historia" style="color: white;">História</a></li>
        <li><a class="menu" href="#seccao-network" style="color: white;">Network</a></li>
        <li><a class="menu" href="#seccao-fundadores" style="color: white;">Fundadores</a></li>
        <li><a class="menu" href="contacto.html" style="color: white;">Contacto</a></li>
      </ul>
    </div>
    <div class='nav-bg-fostrap'>
      <div class='navbar-fostrap'> <span></span> <span></span> <span></span> </div>

    </div>
  </div>
</nav>

I am developing this site for a professional aptitude project of my course so any help was welcome. If anyone could help me I was very grateful, thank you.

Adam
  • 1,385
  • 1
  • 10
  • 23
KR4K
  • 45
  • 6
  • 1
    If you combine all those snippets into one in the `<>` editor then we could run that code here in page and see it in action. By separating them into three snippets the css and js is not associated with the html in order to work together when clicking on *"Run code snippet"* – charlietfl May 27 '18 at 16:10
  • Check my answer, I update it. – Saeed May 31 '18 at 05:27

1 Answers1

0

You can use this part of code and also add any other logic you want.

$(document).click(function(e) {
    var target = e.target;
    if (
      !$(target).is(".navbar-fostrap") &&
      !$(target).parents(".navbar-fostrap").length &&
      !$(target).parents(".nav-fostrap").length
    ) {
      $(".nav-fostrap").removeClass("visible");
      $("body").removeClass("cover-bg");
    }
});

Full code here

$(document).ready(function() {
  $(".navbar-fostrap").click(function() {
    $(".nav-fostrap").toggleClass("visible");
    $("body").toggleClass("cover-bg");
  });
  $(document).click(function(e) {
    var target = e.target;
    if (!$(target).is(".navbar-fostrap") &&
      !$(target).parents(".navbar-fostrap").length &&
      !$(target).parents(".nav-fostrap").length
    ) {
      $(".nav-fostrap").removeClass("visible");
      $("body").removeClass("cover-bg");
    }
  });
});
body {
  background-color: red;
}

nav {
  height: 60px;
  text-align: center;
  position: relative;
  width: width:100%;
  margin: auto;
  z-index: 100000;
}

.navbar-default {
  transition: 500ms ease;
  background: transparent;
}

.navbar-default.scrolled {
  background: rgba(28, 28, 28, 0.8);
}

a.title {
  float: left;
  font-size: 25px;
  color: white;
  font-family: quantum;
  height: 55px;
  padding-top: 2px;
}

.title {
  float: left;
  display: inline-block;
  padding: 0;
  margin-top: 5px;
  list-style: none;
  height: 55px;
}

.nav-fostrap ul {
  list-style-type: none;
  margin-top: 5px;
  padding: 0;
  display: block;
  float: right;
  width: auto;
}

.nav-fostrap ul li {
  display: inline-block;
  font-weight: bold;
  text-transform: uppercase;
  text-align: center;
  padding-left: 15px;
  padding-right: 15px;
  margin-top: 0;
  list-style: none;
  height: 60px;
}

a.menu {
  font-family: Montserrat, Arial;
  font-weight: 700;
  position: relative;
  display: inline-block;
  cursor: pointer;
  padding: 8px 0;
  margin-top: 14.5px;
  margin-bottom: 10.5px;
  color: #fff;
  text-decoration: none;
  height: 40px;
  top: -10px;
}

a:hover.menu {
  color: #fff;
  text-decoration: none;
}

.menu a:before,
a:after {
  content: '';
  position: absolute;
  width: 0%;
  height: 2px;
  bottom: -2px;
  background: #fff;
}

.menu a:before {
  left: 0;
}

.menu a:after {
  right: 0;
  background: #fff;
  transition: width 0.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:before {
  background: #fff;
  width: 100%;
  transition: width 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:after {
  background: transparent;
  width: 100%;
  transition: 0s;
}

.title-on-mobile {
  display: none;
}

@media only screen and (max-width:768px) {
  a.title {
    float: none;
    font-size: 25px;
    color: white;
    font-family: quantum;
    height: 55px;
    padding-top: 2px;
    width: 100%;
    text-align: center;
    position: fixed;
    left: -50px;
  }
  .nav-fostrap {
    background: #1c1c1c;
    width: 200px;
    height: 100%;
    display: block;
    position: fixed;
    left: -200px;
    top: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    margin: 0;
    border: 0;
    border-radius: 0;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
    z-index: 105;
  }
  .nav-fostrap.visible {
    left: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    z-index: 90;
  }
  .nav-bg-fostrap {
    display: inline-block;
    vertical-align: middle;
    width: 100%;
    height: 60px;
    margin: 0;
    position: absolute;
    top: 0px;
    left: 0px;
    background: transperant;
    padding: 12px 0 0 10px;
    -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -ms-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -o-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    z-index: 100;
  }
  .navbar-fostrap {
    display: inline-block;
    vertical-align: middle;
    height: 50px;
    cursor: pointer;
    margin-top: 5px;
    position: absolute;
    top: 0;
    left: 0;
    padding: 12px;
    z-index: 102;
  }
  .navbar-fostrap span {
    height: 2px;
    background: #fff;
    margin: 5px;
    display: block;
    width: 20px;
  }
  .navbar-fostrap span:nth-child(2) {
    width: 20px;
  }
  .navbar-fostrap span:nth-child(3) {
    width: 20px;
  }
  .nav-fostrap ul {
    padding-top: 50px;
  }
  .nav-fostrap li {
    display: block;
  }
  .nav-fostrap li a {
    display: block;
    color: #505050;
    font-weight: 600;
  }
  .nav-fostrap li:first-child:hover a {
    border-radius: 0;
  }
  .cover-bg {
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav class="menu navbar navbar-default navbar-fixed-top">
  <div class="headercontainer">
    <a class="title" href="#seccao-inicio"><img src="img/logo.png" alt="logo" width="80" href="#" style="position: relative;margin-left: 12px; float:right; top:2.5px;position: relative;" /></a>

    <div class='nav-fostrap'>
      <ul class="menu">
        <li><a class="menu" href="#seccao-historia" style="color: white;">História</a></li>
        <li><a class="menu" href="#seccao-network" style="color: white;">Network</a></li>
        <li><a class="menu" href="#seccao-fundadores" style="color: white;">Fundadores</a></li>
        <li><a class="menu" href="contacto.html" style="color: white;">Contacto</a></li>
      </ul>
    </div>
    <div class='nav-bg-fostrap'>
      <div class='navbar-fostrap'> <span></span> <span></span> <span></span> </div>

    </div>
  </div>
</nav>

Hope it helps.

Saeed
  • 5,413
  • 3
  • 26
  • 40
  • Uau thanks a lot! :D Just one more thing if possible, you can put the menu when it opens to not return to the top of the site and when I click on a menu item for example the "Founders" it closes and makes a smooth scroll. You can see the example here, thank you. http://quantumn.atwebpages.com/ – KR4K May 31 '18 at 10:39
  • Read my answer [here](https://stackoverflow.com/questions/50297599/mousemove-scroll-to-next-hash/50343728#50343728) I answer it there. If it does not help, ask me @KR4K – Saeed May 31 '18 at 10:46
  • Unfortunately this did not help I just wanted it when I clicked on the menu it did not go back to the top and when I click on the link it closes itself so I do not have to click to close it and only then to scroll to the section that I defined. – KR4K May 31 '18 at 10:54
  • Ok. give me time. can you provide a working exampe on plunker or codepen?? @KR4K – Saeed May 31 '18 at 10:56
  • CodePen if its possible. Thanks for helping me. – KR4K May 31 '18 at 11:00
  • Yes it is here. The only link that really works is História, in this example. https://codepen.io/Pro90_PT/pen/pKJjQX – KR4K May 31 '18 at 11:51
  • Omg worked, very thankful that was it! But only one thing that part of the code has changed? CSS, HTML OR JS? – KR4K May 31 '18 at 12:08
  • Look at js part. I also put codes for closing navbar in `a` click @KR4K – Saeed May 31 '18 at 12:15
  • Yes, I have, thank you. But how can I stop him from returning to the top when I click to open the menu? – KR4K May 31 '18 at 12:19
  • Okay, when you change, please comment. Thanks for all the help. But until it's working so hard. – KR4K May 31 '18 at 12:27
  • Check it again! I changed it. @KR4K – Saeed May 31 '18 at 12:36
  • Nice its great now! Thanks for helping me! :D – KR4K May 31 '18 at 13:31
  • Only one thing I know I'm annoying that I do not talk a bit but as I saw you're a king of JavaScript you can take a look at this my other project yesterday hear a user who helped me only have a problem I did not want it when this refresh the news with the tags change of place type as if a random mode was on. You can see it here if you do not mind. https://stackoverflow.com/questions/50568350/make-recent-posts-label-box-using-li-childs-javascript/50569116?noredirect=1#comment88191628_50569116 – KR4K May 31 '18 at 13:40