侧导航的菜单图标

时间:2017-09-28 15:52:40

标签: javascript html

菜单图标在点击时打开侧面导航,图标变为“X”。在第二次单击时,我希望它关闭侧面导航并返回到正常菜单图标。

我有以下代码:

<!DOCTYPE html>
<html>
<head>
<style>
.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

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

.sidenav a:hover {
    color: #f1f1f1;
}

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

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

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}

.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}

.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
    transform: rotate(-45deg) translate(-9px, 6px) ;
}

.change .bar2 {opacity: 0;}

.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px) ;
    transform: rotate(45deg) translate(-8px, -8px) ;
}

</style>

</head>
<body>

<div id="mySidenav" class="sidenav">
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <div class="container" style="font-size:30px;cursor:pointer" onclick="openNav(); myFunction(this)">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
</div>
</div>

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

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
}

function myFunction(x) {
    x.classList.toggle("change");
}


</script>

</body>
</html> 

它目前打开侧面导航很好,但我不知道如何告诉它在第二次点击时关闭侧面导航,或者在哪里放置我的closeNav()功能。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以使用班级切换。一个类可以是使用x图标关闭的菜单,当您切换另一个类时,您可以指定相反的。

&#13;
&#13;
document.getElementById('menu').addEventListener('click', function () {
  if (this.classList.contains('open')) {
   this.classList.remove('open');
  } else {
   this.classList.add('open');
  }
});
&#13;
#menu {
 background:red;
 width:100px;
 height:200px;
}


#menu.open {
width:250px;
background:blue;
}
&#13;
<div id="menu">
 <p>click me!</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我不喜欢添加JQuery,如果它还没有被使用......你可以通过检查菜单的状态来做到这一点。

<div id="mySidenav" class="sidenav">
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <div class="container" style="font-size:30px;cursor:pointer" onclick="toggleMenu(this)">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
</div>
</div>

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

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
}

function toggleMenu(x) {
    x.classList.toggle("change");
    if(document.getElementById("mySidenav").style.width > "1px"){
    closeNav();
    } else {
    openNav();
    }
}
</script>