点击任意位置关闭侧导航栏javascript

时间:2017-10-20 18:23:24

标签: javascript html css twitter-bootstrap

我在bootstrap中使用下面的代码片段在导航栏中显示侧边栏。

我可以通过单击侧栏中的X按钮来关闭导航栏。如何通过点击页面上的任意位置以及仅使用javascript的关闭按钮来更改我的closeNav javascript以关闭侧边栏?

    function openNav() {
      document.getElementById("mySidenav").style.width = "230px";
      document.getElementById("main").style.marginLeft = "250px";
      }

  /* Set the width of the side navigation to 0 and the left margin of the page content to 0 */
  function closeNav() {
      document.getElementById("mySidenav").style.width = "0";
      document.getElementById("main").style.marginLeft = "0";
      }
  
/* The side navigation menu */
.sidenav {
    height: 100%; /* 100% Full-height */
    width: 0; /* 0 width - change this with JavaScript */
    position: fixed; /* Stay in place */
    z-index: 1; /* Stay on top */
    top: 0;
    left: 0;
    background-color: #111; /* Black*/
    overflow-x: hidden; /* Disable horizontal scroll */
    padding-top: 60px; /* Place content 60px from the top */
    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
    transition: margin-left .5s;
    padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<!-- Use any element to open the sidenav -->
<span onclick="openNav()">Siderbar Button click here</span>

<div id="main">
Body content is here</div>

4 个答案:

答案 0 :(得分:4)

首先要做的事情。我建议你不要在HTML中使用onclick属性,这就是创建eventlisteners的原因

document.querySelector('span#open_menu').addEventListener('click', 
function(){
   openNav();
})

document.querySelector('. closebtn').addEventListener('click', 
function(){
   closeNav();
})

其次,只需以相同的方式向您的#main div添加另一个事件监听器,请记住,您可以使用不同的元素触发相同的事件而不会出现任何问题

document.querySelector('#main').addEventListener('click',
   closeNav();
})

请记住在HTML中使用始终事件侦听器而不使用JS!

答案 1 :(得分:3)

只需向div添加一个事件监听器:

document.getElementById('main').addEventListener('click', closeNav);

function openNav() {
  document.getElementById("mySidenav").style.width = "230px";
  document.getElementById("main").style.marginLeft = "250px";
}

/* Set the width of the side navigation to 0 and the left margin of the page content to 0 */
function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}
/* The side navigation menu */

.sidenav {
  height: 100%;
  /* 100% Full-height */
  width: 0;
  /* 0 width - change this with JavaScript */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Stay on top */
  top: 0;
  left: 0;
  background-color: #111;
  /* Black*/
  overflow-x: hidden;
  /* Disable horizontal scroll */
  padding-top: 60px;
  /* Place content 60px from the top */
  transition: 0.5s;
  /* 0.5 second transition effect to slide in the sidenav */
}


/* The navigation menu links */

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s
}


/* When you mouse over the navigation links, change their color */

.sidenav a:hover,
.offcanvas a:focus {
  color: #f1f1f1;
}


/* Position and style the close button (top right corner) */

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}


/* Style page content - use this if you want to push the page content to the right when you open the side navigation */

#main {
  transition: margin-left .5s;
  padding: 20px;
}


/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */

@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<!-- Use any element to open the sidenav -->
<span onclick="openNav()">Siderbar Button click here</span>

<div id="main" style="height:100vh;">
  Body content is here
</div>

答案 2 :(得分:0)

只需将closeNav附加到正文并停止在openNav中传播事件。

&#13;
&#13;
function openNav(event) {
      event.stopPropagation();
      document.getElementById("mySidenav").style.width = "230px";
      document.getElementById("main").style.marginLeft = "250px";
      document.getElementsByTagName('body')[0].style.background = "grey";
      }

  /* Set the width of the side navigation to 0 and the left margin of the page content to 0 */
  function closeNav() {
      document.getElementById("mySidenav").style.width = "0";
      document.getElementById("main").style.marginLeft = "0";
      document.getElementsByTagName('body')[0].style.background = "white";
      }
&#13;
/* The side navigation menu */
body {
  height: 100vh;
  width: 100%;
}
.sidenav {
    height: 100%; /* 100% Full-height */
    width: 0; /* 0 width - change this with JavaScript */
    position: fixed; /* Stay in place */
    z-index: 1; /* Stay on top */
    top: 0;
    left: 0;
    background-color: #111; /* Black*/
    overflow-x: hidden; /* Disable horizontal scroll */
    padding-top: 60px; /* Place content 60px from the top */
    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
    transition: margin-left .5s;
    padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}
&#13;
<body onclick="closeNav()">
<div id="mySidenav" class="sidenav" onclick="event.stopPropagation();">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<!-- Use any element to open the sidenav -->
  <span onclick="openNav(event)">Siderbar Button click here</span>
  <div id="main">Body content is here</div>
</body>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

这是我的方法 - 基本上只是将另一个事件监听器添加到主区域,如果菜单打开则关闭它,否则不执行任何操作:

另外......在你的直播网站上,我会改变

var mainArea = document.querySelector("#main");

为:

var mainArea = document.querySelector("body");

获得最佳效果

var openButton = document.querySelector(".open-button");
var closeButton = document.querySelector(".closebtn");
var mainArea = document.querySelector("#main");
var menuIsOpen;

openButton.addEventListener("click", function() {
	menuIsOpen = true;
	document.getElementById("mySidenav").style.width = "230px";
  document.getElementById("main").style.marginLeft = "250px";
});

closeButton.addEventListener("click", function(e) {
  menuIsOpen = false;
	document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
});

mainArea.addEventListener("click", function(e) {
  if (menuIsOpen == true) {
  	document.getElementById("mySidenav").style.width = "0";
  	document.getElementById("main").style.marginLeft = "0";	
  }
});
/* The side navigation menu */
.sidenav {
    height: 100%; /* 100% Full-height */
    width: 0; /* 0 width - change this with JavaScript */
    position: fixed; /* Stay in place */
    z-index: 1; /* Stay on top */
    top: 0;
    left: 0;
    background-color: #111; /* Black*/
    overflow-x: hidden; /* Disable horizontal scroll */
    padding-top: 60px; /* Place content 60px from the top */
    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
    transition: margin-left .5s;
    padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<!-- Use any element to open the sidenav -->
<span class="open-button">Siderbar Button click here</span>

<div id="main">
Body content is here</div>