2017-04-04 62 views
3

任何人都可以幫我解決這個問題嗎?因爲我在關閉我的菜單導航欄時遇到了問題。當我點擊x標記時,我只能關閉導航欄。但我想關閉它,當我點擊導航欄之外。有人可以幫我嗎..?如何關閉這個側面菜單導航點擊以外的其他菜單導航div

function openNav() { 
 
    document.getElementById("mySidenav").style.width = "250px"; 
 
    document.getElementById("hehe").style.marginLeft = "250px"; 
 
\t document.getElementById("main").style.marginLeft = "250px"; 
 
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)"; 
 
} 
 

 
function closeNav() { 
 
    document.getElementById("mySidenav").style.width = "0"; 
 
    document.getElementById("hehe").style.marginLeft= "0"; 
 
\t document.getElementById("main").style.marginLeft = "0"; 
 
    document.body.style.backgroundColor = "white"; 
 
} 
 

 

 
var acc = document.getElementsByClassName("accordion"); 
 
var i; 
 

 
for (i = 0; i < acc.length; i++) { 
 
    acc[i].onclick = function() { 
 
    this.classList.toggle("active"); 
 
    var panel = this.nextElementSibling; 
 
    if (panel.style.maxHeight){ 
 
     panel.style.maxHeight = null; 
 
    } else { 
 
     panel.style.maxHeight = panel.scrollHeight + "px"; 
 
    } 
 
    } 
 
}
body { 
 
    font-family: "Lato", sans-serif; 
 
    transition: background-color .5s; 
 
\t background: url(pic/admin2.jpg) no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
    overflow:hidden; 
 
} 
 

 
.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; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s 
 
} 
 

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

 
.sidenav .closebtn { 
 
    position: absolute; 
 
    top: -10px; 
 
    right: 0px; 
 
    font-size: 50px; 
 
} 
 

 
#main { 
 
    transition: margin-left .5s; 
 
    padding: 10px; 
 
\t position: absolute; 
 
    top: 4px; 
 
    left: 4px; 
 
\t position:fixed absolute relative; 
 
\t border-radius: 0px 118px 118px 118px; 
 
-moz-border-radius: 0px 118px 118px 118px; 
 
-webkit-border-radius: 0px 118px 118px 118px; 
 
border: 6px solid #818181; 
 
color: #818181; 
 
} 
 

 
#main:hover 
 
{ 
 
\t border-radius: 0px 118px 118px 118px; 
 
-moz-border-radius: 0px 118px 118px 118px; 
 
-webkit-border-radius: 0px 118px 118px 118px; 
 
border: 6px solid #7CFC00; 
 
\t color: #7CFC00; 
 
} 
 

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

 
.lalala { 
 
\t position: absolute; 
 
\t bottom: 50px; 
 
\t cursor: pointer; 
 
    padding: 18px; 
 
    width: 100%; 
 
    text-align: left; 
 
    border: none; 
 
    outline: none; 
 
    transition: 0.4s; \t 
 
} 
 

 
a.accordion:after { 
 
    content: '\002B'; 
 
    color: #777; 
 
    font-weight: bold; 
 
    float: right; 
 
    margin-left: 5px; 
 
} 
 

 
a.accordion.active:after { 
 
    content: "\2212"; 
 
\t color: #7CFC00; 
 
} 
 

 

 
div.panel { 
 
    padding: 0 25px; 
 
    max-height: 0; 
 
    overflow: hidden; 
 
    transition: max-height 0.2s ease-out; 
 
\t font-size: 15px; 
 

 
} 
 
.navbar { 
 
    overflow: hidden; 
 
    background-color: #333; 
 
    position: fixed; 
 
    top: 0; 
 
    width: 100%; 
 
} 
 

 
.huhu { 
 
    position: relative; 
 
    top: 0px; 
 
    left: 70px; 
 
    font-size: 50px; 
 
\t color:#818181; 
 
} 
 
.text { 
 
    position: relative; 
 
    top: 70px; 
 
    left: 100px; 
 
\t color:#818181; 
 
} 
 
.head{ 
 
\t text-decoration: none; 
 
\t color:#818181; 
 
} 
 
.head:hover{ 
 
\t text-decoration: none; 
 
\t color:#7CFC00; 
 
} 
 
#hehe{ 
 
\t transition: margin-left .5s; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
<div id="mySidenav" class="sidenav"> 
 
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a> 
 
    <a href="admin.php" class="focus">Home</a> 
 
    <a href="#" class="accordion">Department</a> 
 
<div class="panel"> 
 
    <a href="AdminDepartmentList.php">List of department</a> 
 
    <a href="AdminDepartmentAdd.php">Add department</a> 
 
</div> 
 

 
<a href="#" class="accordion">User</a> 
 
<div class="panel"> 
 
    <a href="AdminUserList.php">List of User</a> 
 
    <a href="AdminUserAdd.php">Add User</a> 
 
</div> 
 

 
<a href="#" class="accordion">System & Vendor</a> 
 
<div class="panel"> 
 
    <a href="#">List of System</a> 
 
    <a href="#">List of Vendor</a> 
 
    <a href="#">Records</a> 
 
</div> 
 

 
    <a href="#" >User Log</a> 
 
<div id="lalala" class="lalala"> 
 
    <a href="logoutAdmin.php" >Log Out</a> 
 
</div> 
 
</div> 
 
<div id="main"> 
 
    <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span> 
 
</div> 
 
<div id="hehe"> 
 
<div class="huhu"> 
 
<a href="admin.php" class="head"> 
 
Vendor Records System 
 
</a> 
 
</div> 
 
<div class="text"> 
 
Welcome Admin 
 
</div> 
 
</div> 
 
</body>  
 
</html>

+1

的[我如何檢測一個元素之外的點擊?(可能的複製http://stackoverflow.com/questions/152975/how- do-i-detect-a-click-one-element) –

回答

0

這將幫助你

var overlay = document.querySelector(".overlay"); 
 

 
    function openNav() { 
 
    document.getElementById("mySidenav").style.width = "250px"; 
 
    document.getElementById("mySidenav").style.zIndex = 3; 
 
    document.getElementById("hehe").style.marginLeft = "250px"; 
 
    document.getElementById("main").style.marginLeft = "250px"; 
 
    //document.body.style.backgroundColor = "rgba(0,0,0,0.4)"; 
 
    overlay.classList.add("open"); 
 

 
} 
 

 
function closeNav() { 
 
    document.getElementById("mySidenav").style.width = "0"; 
 
    document.getElementById("mySidenav").style.zIndex = 1; 
 
    document.getElementById("hehe").style.marginLeft= "0"; 
 
    document.getElementById("main").style.marginLeft = "0"; 
 
    overlay.classList.remove("open"); 
 
    //document.body.style.backgroundColor = "white"; 
 
} 
 

 

 
var acc = document.getElementsByClassName("accordion"); 
 
var i; 
 

 
for (i = 0; i < acc.length; i++) { 
 
    acc[i].onclick = function() { 
 
    this.classList.toggle("active"); 
 
    var panel = this.nextElementSibling; 
 
    if (panel.style.maxHeight){ 
 
     panel.style.maxHeight = null; 
 
    } else { 
 
     panel.style.maxHeight = panel.scrollHeight + "px"; 
 
    } 
 
    } 
 
}
body { 
 
    font-family: "Lato", sans-serif; 
 
    transition: background-color .5s; 
 
    background: url(pic/admin2.jpg) no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
    overflow:hidden; 
 
} 
 

 
.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; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s 
 
} 
 

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

 
.sidenav .closebtn { 
 
    position: absolute; 
 
    top: -10px; 
 
    right: 0px; 
 
    font-size: 50px; 
 
} 
 

 
#main { 
 
    transition: margin-left .5s; 
 
    padding: 10px; 
 
    position: absolute; 
 
    top: 4px; 
 
    left: 4px; 
 
    position:fixed absolute relative; 
 
    border-radius: 0px 118px 118px 118px; 
 
-moz-border-radius: 0px 118px 118px 118px; 
 
-webkit-border-radius: 0px 118px 118px 118px; 
 
border: 6px solid #818181; 
 
color: #818181; 
 
} 
 

 
#main:hover 
 
{ 
 
    border-radius: 0px 118px 118px 118px; 
 
-moz-border-radius: 0px 118px 118px 118px; 
 
-webkit-border-radius: 0px 118px 118px 118px; 
 
border: 6px solid #7CFC00; 
 
    color: #7CFC00; 
 
} 
 

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

 
.lalala { 
 
    position: absolute; 
 
    bottom: 50px; 
 
    cursor: pointer; 
 
    padding: 18px; 
 
    width: 100%; 
 
    text-align: left; 
 
    border: none; 
 
    outline: none; 
 
    transition: 0.4s; 
 
} 
 

 
a.accordion:after { 
 
    content: '\002B'; 
 
    color: #777; 
 
    font-weight: bold; 
 
    float: right; 
 
    margin-left: 5px; 
 
} 
 

 
a.accordion.active:after { 
 
    content: "\2212"; 
 
    color: #7CFC00; 
 
} 
 

 

 
div.panel { 
 
    padding: 0 25px; 
 
    max-height: 0; 
 
    overflow: hidden; 
 
    transition: max-height 0.2s ease-out; 
 
    font-size: 15px; 
 

 
} 
 
.navbar { 
 
    overflow: hidden; 
 
    background-color: #333; 
 
    position: fixed; 
 
    top: 0; 
 
    width: 100%; 
 
} 
 

 
.huhu { 
 
    position: relative; 
 
    top: 0px; 
 
    left: 70px; 
 
    font-size: 50px; 
 
    color:#818181; 
 
} 
 
.text { 
 
    position: relative; 
 
    top: 70px; 
 
    left: 100px; 
 
    color:#818181; 
 
} 
 
.head{ 
 
    text-decoration: none; 
 
    color:#818181; 
 
} 
 
.head:hover{ 
 
    text-decoration: none; 
 
    color:#7CFC00; 
 
} 
 
#hehe{ 
 
    transition: margin-left .5s; 
 
} 
 

 
.overlay { 
 
    height: 100%; 
 
    width: 100%; 
 
    position: fixed; 
 
    z-index: 0; 
 
    top: 0; 
 
    left: 0; 
 
    overflow-x: hidden; 
 
    transition: 0.5s; 
 
} 
 

 
.overlay.open { 
 
    z-index: 2; 
 
    background-color: rgba(0, 0, 0, 0.2) 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>slidenav</title> 
 
</head> 
 
<body> 
 
<div onclick="closeNav()"" class="overlay"></div> 
 
<div id="mySidenav" class="sidenav"> 
 
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a> 
 
    <a href="admin.php" class="focus">Home</a> 
 
    <a href="#" class="accordion">Department</a> 
 
    <div class="panel"> 
 
     <a href="AdminDepartmentList.php">List of department</a> 
 
     <a href="AdminDepartmentAdd.php">Add department</a> 
 
    </div> 
 

 
    <a href="#" class="accordion">User</a> 
 
    <div class="panel"> 
 
     <a href="AdminUserList.php">List of User</a> 
 
     <a href="AdminUserAdd.php">Add User</a> 
 
    </div> 
 

 
    <a href="#" class="accordion">System & Vendor</a> 
 
    <div class="panel"> 
 
     <a href="#">List of System</a> 
 
     <a href="#">List of Vendor</a> 
 
     <a href="#">Records</a> 
 
    </div> 
 

 
     <a href="#" >User Log</a> 
 
    <div id="lalala" class="lalala"> 
 
     <a href="logoutAdmin.php" >Log Out</a> 
 
    </div> 
 
</div> 
 

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

 
<div id="hehe"> 
 
    <div class="huhu"> 
 
    <a href="admin.php" class="head"> 
 
    Vendor Records System 
 
    </a> 
 
    </div> 
 
    <div class="text"> 
 
    Welcome Admin 
 
    </div> 
 
</div> 
 

 
</body> 
 
</html>

+0

你在編碼上有什麼變化?我可否知道 ? –

+0

好的,非常感謝您的幫助..我已經做到了。 –

+0

是的,我只是在打開機身後添加

,並添加了整個機身的樣式。 – patelarpan

0

使用的onmouseout屬性上的導航DIV,並致電closeNav方法。僅從你的代碼

段:

<div id="mySidenav" class="sidenav" onmouseout="closeNav()"> 

希望這有助於。我嘗試過它的工作。