需要按钮来显示和隐藏叠加导航

时间:2016-11-03 11:52:23

标签: javascript jquery html css

我正在制作菜单。我有一个打开它的按钮,我也想按下它来关闭它。我有另一个按钮来关闭它,但这不是我之后的事。谁能教育我如何做到这一点?这是我的JSBIN

这是我打开它的方式

<div class="burgerburger" onclick="openNav()">&#9776; open</div>

这是我正在使用的javascript

function openNav() {
    document.getElementById("myNav").style.width = "100%";
}

function closeNav() {
    document.getElementById("myNav").style.width = "0%";
}

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

应该像这样简单,但由你来决定如何确定&#34; navOpened&#34;

<div class="burgerburger" onclick="toggleNav('myNav')">&#9776; open</div>

JS

function toggleNav(selector) {
    if(navOpened(selector)) {
        closeNav(selector);
    } else {
        openNav(selector);
   }
}     

function navOpened(selector) {
    var elWidth = document.getElementById(selector).style.width;
    if(elWidth == "100%") { 
        return true; 
    } else { 
        return false; 
    }
}

function openNav(selector) {
    document.getElementById(selector).style.width = "100%";
}

function closeNav(selector) {
    document.getElementById(selector).style.width = "0%";
}

答案 1 :(得分:0)

试试此代码

$(function(){
	var is_open = 0;
  $(".burgerburger").click(function(){
    if (is_open == 0) {    	
      openNav();     
    } else {    
      closeNav();     
    }
  });
  $(".closebtn").click(function(){
  	closeNav();  
  });
  
  function openNav() {
    document.getElementById("myNav").style.width = "100%";
    $(".burgerburger").html('&#9776; Close');
     is_open = 1;
}

function closeNav() {
    document.getElementById("myNav").style.width = "0%";
    	$(".burgerburger").html('&#9776; Open');
       is_open = 0;
}

});
body {
    margin: 0;
    background-color: black;
    font-family: 'Lato', sans-serif;
    
}

.burgerburger {
  font-size:30px;
  cursor:pointer;
  color: white;
  z-index: 999999999;
  position: relative;
}

.overlay {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

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

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

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>

<div class="burgerburger" >&#9776; Open</div>

在这里演示https://jsfiddle.net/rnjradmu/

答案 2 :(得分:0)

试试这个: -

&#13;
&#13;
function openNav() {
    document.getElementById("myNav").style.height = "100%";
}

function closeNav() {
    document.getElementById("myNav").style.height = "0%";
}
&#13;
body {
    margin: 0;
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

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

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

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
&#13;
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>


<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
&#13;
&#13;
&#13;

&#13;
&#13;
function openNav() {
    document.getElementById("mySidenav").style.display = "block";
}

function closeNav() {
    document.getElementById("mySidenav").style.display = "none";
}
&#13;
body {
    font-family: "Lato", sans-serif;
}

.sidenav {
    display: none;
    height: 100%;
    width: 250px;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    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, .offcanvas a:focus{
    color: #f1f1f1;
}

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

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
&#13;
<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>


<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

我希望这个人能帮助你 请尝试这个jsfiddle代码

<div class="burgerburger" onclick="myFunction()" id="nav" >&#9776; </div>
<div id="menu">
<ul>
<li>home</li>
<li>about</li>
<li>our team</li>
<li>contact</li>
</ul>
</div>


$("#menu").hide();
$("#nav").click(function() {
    $("#menu").toggle();
});

https://jsfiddle.net/jr3myojp/