0

I am developing a super simple site using jQuery and bootstrap libraries but I would like the menu items to jump to the section (already done with anchors). But when I navigate back to the top the menu dropdown is still open. How would I setup up a toggle so it closes on clicking a menu item?

This is my initial code:

$( document ).ready(function() {
  $( ".cross" ).hide();
  $( ".menu" ).hide();
  $( ".hamburger" ).click(function() {
    $( ".menu" ).slideToggle( "slow", function() {
      $( ".hamburger" ).hide();
      $( ".cross" ).show();
    });
  });
  $( ".cross" ).click(function() {
    $( ".menu" ).slideToggle( "slow", function() {
      $( ".cross" ).hide();
      $( ".hamburger" ).show();
    });
  });
});



<button class="hamburger">&#9776;</button>
<button class="cross">&#735;</button>

<div class="menu">
<ul>
<a href="#"><li>Home</li></a>
<a href="#about"><li>Jeff Anderson</li></a>
<a href="#"><li>Hand Built Frames</li></a>
<a href="#"><li>Bike Repairs and Maintenance</li></a>
<a href="#"><li>Frames and Accessories</li></a>
  <a href="#"><li>Contact</li></a>
</ul>
</div>
</div>

Any help greatly appreciated. Thanks.

Rubba
  • 69
  • 7

1 Answers1

0

Anything that gets to the document will hide the dropdown

$(document).click(function(){
  $("#dropdown").hide();
});

Clicks within the dropdown won't make it past the dropdown itself

$("#dropdown").click(function(e){
  e.stopPropagation();
});

I believe adding these code will solve your problem.

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41