点击屏幕上的任何其他位置退出汉堡包菜单

时间:2017-06-26 17:08:50

标签: javascript jquery html hamburger-menu

我希望能够通过点击汉堡包菜单旁边的任何其他地方退出我的汉堡包菜单。现在,你可以关闭它的唯一方法是单击X,但我想要另一个选项来关闭它。

jsfiddle - https://jsfiddle.net/1h3eoewa/

jQuery(document).ready(function() {
	jQuery(".submenu").slideUp("fast");
  jQuery(".menutoggle").click(function(){
  	var id = jQuery(this).attr("id").replace("toggle","submenu");
  	jQuery("#" + id).slideToggle("fast");    
  })
  
  
  jQuery('#hamburger-nav-icon').click(function() {
    jQuery(this).toggleClass('open');
      jQuery('body').css('overflow','hidden');  
    if (jQuery('#hamburger-menu-cover').css('opacity') == '0') {
      jQuery('#hamburger-menu-cover').css('opacity', '1');
      jQuery('#hamburger-menu-cover').fadeIn(300).css('display', 'table'); 
        jQuery('body').css('overflow','hidden');
    } else {
      jQuery('#hamburger-menu-cover').css('opacity', '0');
      jQuery('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
        jQuery('body').css('overflow','visible');
    }
  });
});
/*--------------------------------------------------------------
## Hamburger Menu
--------------------------------------------------------------*/
#hamburger-menu-cover {
    z-index: 5;
    width: 100%;
    height: 100%;
  background: rgba(255, 255, 255, 0.9);
    position: fixed;
    font-size: 1.5em;
    text-align: center;
    right: 0px;
    top: 0px;
    opacity: 0;
    display: none;
    overflow-y: scroll;
   -webkit-overflow-scrolling: touch;
    overflow: hidden;
}
.hamburger-menu {
background:#fff;
   width: 432px;
    height: 100%;
  border-right: 1px solid #808080;
  position: fixed;
  top: 0px;
      overflow-y: scroll;
  -webkit-overflow-scrolling:touch; // mobile safari
}
.hamburger-menu-container {
  margin-left: 33px;
  margin-top: 100px;
  padding-right: 32px;
  padding-bottom: 40px; 
      overflow-y: scroll;
  -webkit-overflow-scrolling:touch; // mobile safari
}
.hamburger-menu-links li  {
    list-style: none;
      font-family: 'freightlight';
  font-size: 36px;
  display: block;
  line-height: 57px;
    font-style: italic;
    text-transform: uppercase;
    text-decoration: none;
    text-align: justify;
}
.hamburger-menu-links li a {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:hover {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:active {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:visited {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links ul {
          border-bottom: 1px solid #808080;
    padding-bottom: 30px;
}
.submenu li {
  font-size: 16px;
  font-family: 'Raleway', sans-serif;
  line-height: 36px;
  font-weight: 600;
  letter-spacing: 2px;
    font-style: normal;
}
.hamburger-menu-social-links h3 {
  margin-left: 40px;
      padding-right: 32px;
  margin-top: 47px;
  padding-bottom: 0px; 
        list-style: none;
      font-family: 'freightlight';
  font-size: 18px;
  display: block;
    font-style: italic;
    text-transform: uppercase;
    text-decoration: none;
    text-align: justify;
     letter-spacing: 0px;
}
.hamburger-menu-social-links {
    padding-right: 0px;
    letter-spacing: 12px;
}

#hamburger-nav-icon {
    z-index: 20;
  width: 50px;
  height: 35px;
  position: relative;
  margin: 35px 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#hamburger-nav-icon span {
  display: block;
  position: absolute;
  height: 5px;
  width: 40px;
  background: #000;
  opacity: 1;
  left: 0;
}

/* Icon 3 */

#hamburger-nav-icon span:nth-child(1) {
  top: 0px;
}

#hamburger-nav-icon span:nth-child(2),#hamburger-nav-icon span:nth-child(3) {
  top: 12px;
}

#hamburger-nav-icon span:nth-child(4) {
  top: 24px;
}

#hamburger-nav-icon.open span:nth-child(1) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#hamburger-nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#hamburger-nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#hamburger-nav-icon.open span:nth-child(4) {
  top: 8px;
  width: 0%;
  left: 50%;
}
#toggle1, #toggle2, #toggle4, #toggle7, #toggle8 {
    font-size: 12px;
    padding-left:40px;
    padding-top:25px;
    display: inline;
    position: absolute;
    color: #B2B2B2;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hamburger-nav-icon">
              <span></span>
              <span></span>
              <span></span>
              <span></span>
            </div>
        <div id="hamburger-menu-cover">
            <div class="hamburger-menu">
                <div class="hamburger-menu-container">
                <div class="hamburger-menu-links">
            <ul>
                <li><a href="#">Item 1<i class="fa fa-ellipsis-h menutoggle" id="toggle1" aria-hidden="true"></i></a></li>
<div class="submenu" id="submenu1">
                <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                <li><a href="#">Sub Item 3</a></li>
                <li><a href="#">Sub Item 4</a></li>
                    </div>
                <li><a href="#">Item 2<i class="fa fa-ellipsis-h menutoggle" id="toggle2" aria-hidden="true"></i></a></li>
                <div class="submenu" id="submenu2">
                <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                <li><a href="#">Sub Item 3</a></li>
                <li><a href="#">Sub Item 4</a></li>
                    </div>
                <li><a href="#">Item 3</a></li>
                <li><a href="#">Item 4<i class="fa fa-ellipsis-h menutoggle" id="toggle4" aria-hidden="true"></i></a></li>
<div class="submenu" id="submenu4">
                <li><a href="#">Sub Item 2</a></li>
                <li><a href="#">Sub Item 3</a></li>
                <li><a href="#">Sub Item 4</a></li>
                    </div>
                <li><a href="#">Item 5</a></li>
                <li><a href="#">Item 6</a></li>
                <li><a href="#">Item 7<i class="fa fa-ellipsis-h menutoggle" id="toggle7" aria-hidden="true"></i></a></li>
                <div class="submenu" id="submenu7">
                <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                <li><a href="#">Sub Item 3</a></li>
                <li><a href="#">Sub Item 4</a></li>
                </div>
                <li><a href="#">Item 8<i class="fa fa-ellipsis-h menutoggle" id="toggle8" aria-hidden="true"></i></a></li>
                     <div class="submenu" id="submenu8">
                <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                <li><a href="#">Sub Item 3</a></li>
                </div>
            </ul>
                    </div>
                     <div class="hamburger-menu-social-links">
      <h3>Social</h3>
<a href="#" target="blank" title="twitter">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-twitter.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png'" /></a>
<a href="#" target="blank" title="Facebook"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-facebook.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png'" /></a>
<a href="#" target="blank" title="Pinterest"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-pinterest.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png'" /></a>
<a href="#" target="blank" title="Instagram">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-instagram.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png'" />
</a>
<a href="#" target="blank" title="Snapchat"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-snapchat.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png'" /></a>
<a href="#" target="blank" title="Youtube"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-youtube.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png'" /></a>
<a href="#" target="blank" title="Bloglovin"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-bloglovin.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png'" />
</a>
                    </div>
                    
                    
                </div>
            </div>
</div>

1 个答案:

答案 0 :(得分:0)

好的,让我先解决问题。您需要一些代码来捕获文档中的所有点击事件,但#hamburger-nav-icon上的事件除外。发生这种情况时,请检查#hamburger-nav-icon是否包含班级open。如果是,则触发点击#hamburger-nav-icon

所以看起来像这样,对吧?

$(document).click(function(event) {
  if ($('#hamburger-nav-icon').hasClass('open')) {
    if ($(event.target).is('#hamburger-nav-icon')) {
    } else {
      $('#hamburger-nav-icon').click();
    }
  }
});

嗯,不是真的。

问题在于,当你点击除#hamburger-nav-icon div之外的任何地方时,情况会很好,但是当你点击内部里面 div时)。因此,当点击文档时,您需要检查目标是#hamburger-nav-icon 还是 #hamburger-nav-icon span(换句话说,#hamburger-nav-icon的所有范围内的子项)

有两种方法可以解决这个问题,但有一种方法更长,占用的空间更多,但无论如何我都会告诉你。

第一种是使用if / else if / else语句:

$(document).click(function(event) {
  if ($('#hamburger-nav-icon').hasClass('open')) {
    if ($(event.target).is('#hamburger-nav-icon')) {
    //I forgot to add these two lines
    } else if ($(event.target).is('.hamburger-menu')) { 
    } else if ($(event.target).is('.hamburger-menu *')) {
    } else if ($(event.target).is('#hamburger-nav-icon span')) {
    } else {
      $('#hamburger-nav-icon').click();
    }
  }
});

第二种方法是使用我称之为if或/ else的语句:

$(document).click(function(event) {
  if ($('#hamburger-nav-icon').hasClass('open')) {
    if ($(event.target).is('#hamburger-nav-icon')||$(event.target).is('#hamburger-nav-icon span')||$(event.target).is('#hamburger-menu')||$(event.target).is('#hamburger-menu *')) {
    } else {
      $('#hamburger-nav-icon').click();
    }
  }
});

(在JavaScript中,||表示或,和&amp;&amp;表示和。)

所以你有它。下面有两个代码片段,第一个使用第一个方法,第二个使用第二个方法。

&#13;
&#13;
$(document).ready(function() {
  $(".submenu").slideUp("fast");
  $(".menutoggle").click(function() {
    var id = $(this).attr("id").replace("toggle", "submenu");
    $("#" + id).slideToggle("fast");
  })
  $('#hamburger-nav-icon').click(function() {
    $(this).toggleClass('open');
    $('body').css('overflow', 'hidden');
    if ($('#hamburger-menu-cover').css('opacity') == '0') {
      $('#hamburger-menu-cover').css('opacity', '1');
      $('#hamburger-menu-cover').fadeIn(300).css('display', 'table');
      $('body').css('overflow', 'hidden');
    } else {
      $('#hamburger-menu-cover').css('opacity', '0');
      $('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
      $('body').css('overflow', 'visible');
    }
  });
});
$(document).click(function(event) {
  if ($('#hamburger-nav-icon').hasClass('open')) {
    if ($(event.target).is('#hamburger-nav-icon')) {
    } else if ($(event.target).is('.hamburger-menu')) {
    } else if ($(event.target).is('.hamburger-menu *')) {
    } else if ($(event.target).is('#hamburger-nav-icon span')) {
    } else {
      $('#hamburger-nav-icon').click();
    }
  }
});
&#13;
/*--------------------------------------------------------------
## Hamburger Menu
--------------------------------------------------------------*/

#hamburger-menu-cover {
  z-index: 5;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.9);
  position: fixed;
  font-size: 1.5em;
  text-align: center;
  right: 0px;
  top: 0px;
  opacity: 0;
  display: none;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  overflow: hidden;
}

.hamburger-menu {
  background: #fff;
  width: 432px;
  height: 100%;
  border-right: 1px solid #808080;
  position: fixed;
  top: 0px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch; // mobile safari
}

.hamburger-menu-container {
  margin-left: 33px;
  margin-top: 100px;
  padding-right: 32px;
  padding-bottom: 40px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch; // mobile safari
}

.hamburger-menu-links li {
  list-style: none;
  font-family: 'freightlight';
  font-size: 36px;
  display: block;
  line-height: 57px;
  font-style: italic;
  text-transform: uppercase;
  text-decoration: none;
  text-align: justify;
}

.hamburger-menu-links li a {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:hover {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:active {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:visited {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links ul {
  border-bottom: 1px solid #808080;
  padding-bottom: 30px;
}

.submenu li {
  font-size: 16px;
  font-family: 'Raleway', sans-serif;
  line-height: 36px;
  font-weight: 600;
  letter-spacing: 2px;
  font-style: normal;
}

.hamburger-menu-social-links h3 {
  margin-left: 40px;
  padding-right: 32px;
  margin-top: 47px;
  padding-bottom: 0px;
  list-style: none;
  font-family: 'freightlight';
  font-size: 18px;
  display: block;
  font-style: italic;
  text-transform: uppercase;
  text-decoration: none;
  text-align: justify;
  letter-spacing: 0px;
}

.hamburger-menu-social-links {
  padding-right: 0px;
  letter-spacing: 12px;
}

#hamburger-nav-icon {
  z-index: 20;
  width: 50px;
  height: 35px;
  position: relative;
  margin: 35px 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#hamburger-nav-icon span {
  display: block;
  position: absolute;
  height: 5px;
  width: 40px;
  background: #000;
  opacity: 1;
  left: 0;
}


/* Icon 3 */

#hamburger-nav-icon span:nth-child(1) {
  top: 0px;
}

#hamburger-nav-icon span:nth-child(2),
#hamburger-nav-icon span:nth-child(3) {
  top: 12px;
}

#hamburger-nav-icon span:nth-child(4) {
  top: 24px;
}

#hamburger-nav-icon.open span:nth-child(1) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#hamburger-nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#hamburger-nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#hamburger-nav-icon.open span:nth-child(4) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#toggle1,
#toggle2,
#toggle4,
#toggle7,
#toggle8 {
  font-size: 12px;
  padding-left: 40px;
  padding-top: 25px;
  display: inline;
  position: absolute;
  color: #B2B2B2;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="hamburger-nav-icon">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
<div id="hamburger-menu-cover">
  <div class="hamburger-menu">
    <div class="hamburger-menu-container">
      <div class="hamburger-menu-links">
        <ul>
          <li><a href="#">Item 1<i class="fa fa-ellipsis-h menutoggle" id="toggle1" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu1">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 2<i class="fa fa-ellipsis-h menutoggle" id="toggle2" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu2">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 3</a></li>
          <li><a href="#">Item 4<i class="fa fa-ellipsis-h menutoggle" id="toggle4" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu4">
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 5</a></li>
          <li><a href="#">Item 6</a></li>
          <li><a href="#">Item 7<i class="fa fa-ellipsis-h menutoggle" id="toggle7" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu7">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 8<i class="fa fa-ellipsis-h menutoggle" id="toggle8" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu8">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
          </div>
        </ul>
      </div>
      <div class="hamburger-menu-social-links">
        <h3>Social</h3>
        <a href="#" target="blank" title="twitter">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-twitter.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png'" /></a>
        <a href="#" target="blank" title="Facebook"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-facebook.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png'" /></a>
        <a href="#" target="blank" title="Pinterest"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-pinterest.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png'" /></a>
        <a href="#" target="blank" title="Instagram">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-instagram.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png'" />
</a>
        <a href="#" target="blank" title="Snapchat"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-snapchat.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png'" /></a>
        <a href="#" target="blank" title="Youtube"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-youtube.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png'" /></a>
        <a href="#" target="blank" title="Bloglovin"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-bloglovin.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png'" />
</a>
      </div>


    </div>
  </div>
</div>
&#13;
&#13;
&#13;

&#13;
&#13;
$(document).ready(function() {
  $(".submenu").slideUp("fast");
  $(".menutoggle").click(function() {
    var id = $(this).attr("id").replace("toggle", "submenu");
    $("#" + id).slideToggle("fast");
  })
  $('#hamburger-nav-icon').click(function() {
    $(this).toggleClass('open');
    $('body').css('overflow', 'hidden');
    if ($('#hamburger-menu-cover').css('opacity') == '0') {
      $('#hamburger-menu-cover').css('opacity', '1');
      $('#hamburger-menu-cover').fadeIn(300).css('display', 'table');
      $('body').css('overflow', 'hidden');
    } else {
      $('#hamburger-menu-cover').css('opacity', '0');
      $('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
      $('body').css('overflow', 'visible');
    }
  });
});
$(document).click(function(event) {
  if ($('#hamburger-nav-icon').hasClass('open')) {
    if ($(event.target).is('#hamburger-nav-icon')||$(event.target).is('#hamburger-nav-icon span')||$(event.target).is('.hamburger-menu')||$(event.target).is('.hamburger-menu *')) {
    } else {
      $('#hamburger-nav-icon').click();
    }
  }
});
&#13;
/*--------------------------------------------------------------
## Hamburger Menu
--------------------------------------------------------------*/

#hamburger-menu-cover {
  z-index: 5;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.9);
  position: fixed;
  font-size: 1.5em;
  text-align: center;
  right: 0px;
  top: 0px;
  opacity: 0;
  display: none;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  overflow: hidden;
}

.hamburger-menu {
  background: #fff;
  width: 432px;
  height: 100%;
  border-right: 1px solid #808080;
  position: fixed;
  top: 0px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch; // mobile safari
}

.hamburger-menu-container {
  margin-left: 33px;
  margin-top: 100px;
  padding-right: 32px;
  padding-bottom: 40px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch; // mobile safari
}

.hamburger-menu-links li {
  list-style: none;
  font-family: 'freightlight';
  font-size: 36px;
  display: block;
  line-height: 57px;
  font-style: italic;
  text-transform: uppercase;
  text-decoration: none;
  text-align: justify;
}

.hamburger-menu-links li a {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:hover {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:active {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links li a:visited {
  text-decoration: none;
  color: #000;
}

.hamburger-menu-links ul {
  border-bottom: 1px solid #808080;
  padding-bottom: 30px;
}

.submenu li {
  font-size: 16px;
  font-family: 'Raleway', sans-serif;
  line-height: 36px;
  font-weight: 600;
  letter-spacing: 2px;
  font-style: normal;
}

.hamburger-menu-social-links h3 {
  margin-left: 40px;
  padding-right: 32px;
  margin-top: 47px;
  padding-bottom: 0px;
  list-style: none;
  font-family: 'freightlight';
  font-size: 18px;
  display: block;
  font-style: italic;
  text-transform: uppercase;
  text-decoration: none;
  text-align: justify;
  letter-spacing: 0px;
}

.hamburger-menu-social-links {
  padding-right: 0px;
  letter-spacing: 12px;
}

#hamburger-nav-icon {
  z-index: 20;
  width: 50px;
  height: 35px;
  position: relative;
  margin: 35px 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#hamburger-nav-icon span {
  display: block;
  position: absolute;
  height: 5px;
  width: 40px;
  background: #000;
  opacity: 1;
  left: 0;
}


/* Icon 3 */

#hamburger-nav-icon span:nth-child(1) {
  top: 0px;
}

#hamburger-nav-icon span:nth-child(2),
#hamburger-nav-icon span:nth-child(3) {
  top: 12px;
}

#hamburger-nav-icon span:nth-child(4) {
  top: 24px;
}

#hamburger-nav-icon.open span:nth-child(1) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#hamburger-nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#hamburger-nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#hamburger-nav-icon.open span:nth-child(4) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#toggle1,
#toggle2,
#toggle4,
#toggle7,
#toggle8 {
  font-size: 12px;
  padding-left: 40px;
  padding-top: 25px;
  display: inline;
  position: absolute;
  color: #B2B2B2;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="hamburger-nav-icon">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
<div id="hamburger-menu-cover">
  <div class="hamburger-menu">
    <div class="hamburger-menu-container">
      <div class="hamburger-menu-links">
        <ul>
          <li><a href="#">Item 1<i class="fa fa-ellipsis-h menutoggle" id="toggle1" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu1">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 2<i class="fa fa-ellipsis-h menutoggle" id="toggle2" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu2">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 3</a></li>
          <li><a href="#">Item 4<i class="fa fa-ellipsis-h menutoggle" id="toggle4" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu4">
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 5</a></li>
          <li><a href="#">Item 6</a></li>
          <li><a href="#">Item 7<i class="fa fa-ellipsis-h menutoggle" id="toggle7" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu7">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
            <li><a href="#">Sub Item 4</a></li>
          </div>
          <li><a href="#">Item 8<i class="fa fa-ellipsis-h menutoggle" id="toggle8" aria-hidden="true"></i></a></li>
          <div class="submenu" id="submenu8">
            <li><a href="#">Sub Item 1</a></li>
            <li><a href="#">Sub Item 2</a></li>
            <li><a href="#">Sub Item 3</a></li>
          </div>
        </ul>
      </div>
      <div class="hamburger-menu-social-links">
        <h3>Social</h3>
        <a href="#" target="blank" title="twitter">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-twitter.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-twitter.png'" /></a>
        <a href="#" target="blank" title="Facebook"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-facebook.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-facebook.png'" /></a>
        <a href="#" target="blank" title="Pinterest"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-pinterest.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-pinterest.png'" /></a>
        <a href="#" target="blank" title="Instagram">
<img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-instagram.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-instagram.png'" />
</a>
        <a href="#" target="blank" title="Snapchat"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-snapchat.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-snapchat.png'" /></a>
        <a href="#" target="blank" title="Youtube"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-youtube.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-youtube.png'" /></a>
        <a href="#" target="blank" title="Bloglovin"><img src="<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png" border="0" onmouseover="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-hover-bloglovin.png'" onmouseout="this.src='<?php echo get_template_directory_uri(); ?>/images/hamburger-bloglovin.png'" />
</a>
      </div>


    </div>
  </div>
</div>
&#13;
&#13;
&#13;

编辑:我忘了在if语句中添加更多条件;一个用于检查元素是否为#hamburger-menu,另一个用于检查元素是否为#hamburger-menu的子元素。我在^^

之上添加了这些行