如何在外部点击关闭侧边栏菜单?

时间:2019-05-18 05:23:47

标签: html css sidebar

如何通过单击外部关闭侧边栏菜单? 实际上,我创建了一个侧边栏菜单,并通过切换按钮打开和关闭它,但是我也想通过单击外部将其关闭。 我尝试在外部单击时关闭一些代码,它可以正常工作,但是当我单击以打开任何子菜单时,它也会关闭。 在外部点击即可关闭的任何解决方案。



a { text-decoration: none; }

div#overlay { display: none; }

a#toggle {
  position: fixed;
  top: 50px;
  left: 1px;
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.4);
  text-align: center;
  color: white;
  display: none;
}

a#toggle i {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

main#content { padding: 10px; }

#menu {
  text-align: center;
  transition: all ease-out 0.3s;
}

#menu a { color: white; }

#menu ul {
  margin: 0;
  padding: 0;
  background-color: rgba(22,160,133, 0.8);
}

#menu ul li {
  display: inline-block;
  position: relative;
}

#menu ul li > a {
  display: inline-block;
  padding: 10px;
}

#menu ul li > a > i {
  margin-left: 15px;
  transition: all ease-out 0.3s;
  -webkit-transition: all ease-out 0.1s;
}

#menu ul li ul {
  display: none;
  position: absolute;
  top: 38px;
  width: 200px;
  text-align: left;
}

#menu ul li ul li { display: block; }

#menu ul li ul li a { display: block; }

#menu ul li:hover > a { background-color: rgba(0, 0, 0, 0.3); }

#menu ul li:hover > a > i { transform: rotateZ(90deg); }

#menu ul li:hover ul { display: block; }
@media screen and (max-width: 767px) {

a#toggle { display: block; }

main#content {
  margin-top: 65px;
  transition: all ease-out 0.3s;
}

#menu {
  position: fixed;
  width: 250px;
  height: 100%;
  top: 91px;
  left: 0;
  overflow: scroll;
  overflow-y: auto;
  background-color: #FFBD33;
  transform: translateX(-250px);
}

#menu ul {
  text-align: left;
  background-color: transparent;
}

#menu ul li { display: block; }

#menu ul li a { display: block; }

#menu ul li a > i { float: right; }

#menu ul li ul {
  display: none;
  position: static;
  width: 100%;
  background-color: rgba(22,160,133, 0.2);
}

#menu ul li:hover > ul { display: none; }

#menu ul li:hover > a > i { transform: rotateZ(0); }

#menu ul li.open > a { background-color: rgba(0, 0, 0, 0.3); }

#menu ul li.open > a > i { transform: rotateZ(90deg); }

#menu ul li.open > ul { display: block; }

div#overlay {
  display: block;
  visibility: hidden;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  transition: all ease-out 0.3s;
  z-index: 1;
  opacity: 0;
}

html.open-menu { overflow: auto; }

html.open-menu div#overlay {
  visibility: visible;
  opacity: 1;
  width: calc(-150%);
  left: 250px;
}

html.open-menu a#toggle,
 html.open-menu main#content { transform: translateX(0px); }

html.open-menu nav#menu {
  z-index: 3;
  transform: translateX(0);
}
}

<script>
(function($){
  var ico = $('<i class="fa fa-caret-right"></i>');
  $('nav#menu li:has(ul) > a').append(ico);

  $('nav#menu li:has(ul)').on('click',function(){
    $(this).toggleClass('open');
  });

  $('a#toggle').on('click',function(e){
    $('html').toggleClass('open-menu');
    return false;
  });

  $('body,html').on('click',function(){
  $('html').removeClass('open-menu');
});
   $('html').on('click',function(e){
    $('html').toggleClass('open-menu');
  });


  $('div#overlay').on('click',function(){
    $('html').removeClass('open-menu');
  })

})(jQuery)
</script>```

1 个答案:

答案 0 :(得分:0)

编辑您的js代码以遵循,也可以根据需要自定义

(function($){
  var ico = $('<i class="fa fa-caret-right"></i>');
  $('nav#menu li:has(ul) > a').append(ico);

  $('nav#menu li:has(ul)').on('click',function(){
    console.log('1');
    $(this).toggleClass('open');
  });

  $('a#toggle').on('click',function(e){
    console.log('2');
    $('html').toggleClass('open-menu');
    return false;
  });

  $('body,html').on('click',function(){
    console.log('3');
  $('html').toggleClass('open-menu');
});


  $('div#overlay').on('click',function(){
    console.log('5');
    $('html').removeClass('open-menu');
  })

})(jQuery)


这是您的工作代码被旧代码所取代