如果我点击很远的话,hamburgermenu复选框不会被取消选中

时间:2017-09-06 11:33:43

标签: jquery hamburger-menu

保持简洁: 如果我点击其他地方,我的汉堡包菜单就不会关闭。 我在jquery中尝试过它并尝试使用removeClass等等,但即使使用removeClass它也没有用。

Here's the code:
https://jsfiddle.net/dtgd551q/

我认为只需取消选中复选框就是最简单的方法。

感谢所有能给我帮助的人。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$(document).on('click', function(e){
 if(e.target.type == "checkbox")
     return;
 else{
    var elem = $('#menuToggle').find('input[type=checkbox]');
    if($(elem).prop('checked')){
      $(elem).trigger('click');
    }
 }   
});
body
{
  overflow-x: hidden;
  margin: 0;
  padding: 0;
  font-family: "Avenir Next", "Avenir", sans-serif;
}

a
{
  text-decoration: none;
  color: white;
  transition: color 0.1s ease;
}

a:hover
{
  color: black;
}
nav {
	display: inline-block;
}
#menuToggle
{
  display: block;
  position: relative;
  top: 50px;
  left: 50px;
  
  z-index: 1;
  
  -webkit-user-select: none;
  user-select: none;
}

#menuToggle input
{
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  left: -5px;
  
  cursor: pointer;
  
  opacity: 0; /* hide this */
  z-index: 2; /* and place it over the hamburger */
  
  -webkit-touch-callout: none;
}

/*
 * Just a quick hamburger
 */
#menuToggle span
{
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  
  background: black;
  border-radius: 3px;
  
  z-index: 1;
  
  transform-origin: 4px 0px;
  
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
              background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
              opacity 0.55s ease;
}

#menuToggle span:first-child
{
  transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2)
{
  transform-origin: 0% 100%;
}

/* 
 * Transform all the slices of hamburger
 * into a crossmark.
 */
#menuToggle input:checked ~ span
{
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: white
}

/*
 * But let's hide the middle one.
 */
#menuToggle input:checked ~ span:nth-last-child(3)
{
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}

/*
 * Ohyeah and the last one should go the other direction
 */
#menuToggle input:checked ~ span:nth-last-child(2)
{
  opacity: 1;
  transform: rotate(-45deg) translate(0, -1px);
}

/*
 * Make this absolute positioned
 * at the top left of the screen
 */
#menu
{
  position: absolute;
  width: 400px;
  margin: -100px 0 0 -50px;
  padding: 50px;
  padding-top: 125px;
  
  background: black;
  list-style-type: none;
  -webkit-font-smoothing: antialiased;
  /* to stop flickering of text in safari */
  
  transform-origin: 0% 0%;
  transform: translate(-100%, 0);
  
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li
{
  padding: 10px 0;
  font-size: 22px;
}

/*
 * And let's fade it in from the left
 */
#menuToggle input:checked ~ ul
{
  transform: scale(1.0, 1.0);
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav role="navigation">
  	<div id="menuToggle">
    	<input type="checkbox" />
    	<span></span>
    	<span></span>
    	<span></span>
    	<ul id="menu">
      <a href="#"><li>Home</li></a>
      <a href="#"><li>About</li></a>
      <a href="#"><li>Info</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="https://erikterwan.com/" target="_blank"><li>Show me more</li></a>
    </ul>
  </div>
</nav>

您可以检测到除checkbox以外的页面任何部分点击,因为点击checkbox已经有效。因此,return内的e.target.type == "checkbox"命令确认单击的元素不是复选框。现在,如果它不是复选框,则检查菜单是否已打开。如果它已打开,那么$(elem).trigger('click');会在checkbox上生成一个关闭它的点击。