简体   繁体   English

单击“其他”时如何隐藏我的滑动侧导航?

[英]How to hide my sliding side nav when i click elsewere?

I'm trying to mimic android's side menu style. 我正在尝试模仿android的侧边菜单样式。 here is the link to my codepen https://codepen.io/Killerbee98/pen/KqreZw . 这是我的codepen https://codepen.io/Killerbee98/pen/KqreZw的链接。 My problem is that it doesn't close when i press outside of the navigation as it does on android. 我的问题是,当我按导航外部按钮时,它无法关闭,就像在android上一样。 Now i have done some research on how to do smth like this; 现在,我对如何做这样的事情做了一些研究。 first would be to use: 首先是使用:

$('#menucontainer').click(function(event){
  event.stopPropagation();
});

but this isn't a practical method and is wrong to use. 但这不是一种实用的方法,使用错误。

Second would be to add a click listener using : 第二种方法是使用添加一个点击监听器:

$(document).click(function(event){ 
  if (!$(event.target).closest('.navigation').length) {
    $('.navigation').hide();
  }
});

But this doesn't work with my code since my code has a click listener: 但这对我的代码不起作用,因为我的代码具有点击监听器:

$('.menu-btn').click(function() {
   console.log("button pressed");
   if($('.navigation').width() === 250){
   $('.navigation').width('0px');
}else{
   $('.navigation').width('250px');
  }
})

i tried doing this: 我尝试这样做:

$('.menu-btn').click(function() {
   console.log("button pressed");
   if($('.navigation').width() === 250){
   $('.navigation').width('0px');
   $(document).click(function(event){ 
       if (!$(event.target).closest('.navigation').length) {
           $('.navigation').hide();
       }
   });
 }else{
   $('.navigation').width('250px');
  }
})

it closes but doesn't open back again as it should. 它会关闭,但不会再次打开。

how do i go about solving my issue? 我该如何解决我的问题?

I Have managed to make a working example by using this answer 我已经设法通过使用这个答案做一个可行的例子

Here is the code 这是代码

 $('.menu-btn').click(function() {
     if($('.navigation').is(":visible")) {
            $('.navigation').hide();
        } else {
           $('.navigation').show();
        }
})

$(document).click(function(event) { 
    if(!$(event.target).closest('.navigation').length &&      
    !$(event.target).hasClass('menu-btn')) {
        if($('.navigation').is(":visible")) {
            $('.navigation').hide();
        }
    }        
})

EDIT: 编辑:

Also I Have adjusted css style for navigation class 我也为navigation类调整了CSS样式

.navigation{
  position:fixed;
  left: 0;
  top: 0;
  width: 250px;
  display: none;
  height: 100%;
  box-shadow: 1px 0px 15px 0px #999;
  transition: .2s;
}

Here is a working demo 这是一个工作演示

The only changes you need to your EXISTING code is to add the same styles for both the button click event and the document click event. 您需要对现有代码进行的唯一更改是为button click事件和document click事件添加相同的样式。

 $('.menu-btn').click(function() { event.stopPropagation(); console.log("button pressed"); if ($('.navigation').width() === 250) { $('.navigation').hide(); $('.navigation').width('0px'); } else { $('.navigation').show(); $('.navigation').width('250px'); } }) $(document).click(function(event) { if (!$(event.target).closest('.navigation').length) { $('.navigation').hide(); $('.navigation').width('0px'); event.stopPropagation(); } }); 
 * { margin: 0; padding: 0; } .navigation { position: fixed; left: 0; top: 0; width: 0; height: 100%; box-shadow: 1px 0px 15px 0px #999; transition: .2s; display: none; } .header-image { height: 150px; background: yellow; margin-bottom: 5px; } ul { list-style-type: none; } ul li { display: inline-block; width: 100%; background: #eee; } ul li a { display: block; width: 100%; height: 50px; } .clearfix:after { content=""; display: table; clear: both; } .menu-btn { height: 50px; width: 50px; background: none; border: none; cursor: pointer; outline: none; position: fixed; left: 0; top: 0; z-index: 100; } .bar { display: block; position: absolute; width: 30px; height: 2px; background: black; left: 10px; right: 10px; } .bar:before, .bar:after { display: block; content: ""; left: 0; width: 100%; background-color: black; position: absolute; height: 2px; transition: .15s ease-in-out; } .bar:before { top: -10px; } .bar:after { top: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="menu-btn"> <span class="bar"></span> </button> <div class="navigation"> <div class="header-image"></div> <ul> <div class="link"> <li> <a href=""></a> </li> </div> <div class="link"> <li> <a href=""></a> </li> </div> <div class="link"> <li> <a href=""></a> </li> </div> <div class="link"> <li> <a href=""></a> </li> </div> </ul> </div> 

Thanks to @Kukic Vladimir's solution, it works with a bit of change in the JS code he provided. 感谢@Kukic Vladimir的解决方案,它可以在他提供的JS代码中进行一些更改。

$('.menu-btn').click(function() {
 if($('.navigation').width() === 0) {
        $('.navigation').width('250px');
    } else {
       $('.navigation').width('0px');
    }
});
$(document).click(function(event) { 
    if(!$(event.target).closest('.navigation').length &&      
!$(event.target).hasClass('menu-btn')) {
    if($('.navigation').width('250px')) {
        $('.navigation').width('0px');
    }
 }        
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM