2016-08-04 90 views
2

您好我想創建一個側面導航,我不能管理與第二功能關閉菜單「的onclick()」我的導航圖標不會關閉

導航打開完美,但隨後它不熟。 這裏是我的小提琴,我只是希望得到一個添加onlick函數,當再次點擊導航圖標時關閉導航的幫助。

function myFunction(x) { 
 
    x.classList.toggle("change"); 
 
    document.getElementById("mySidenav").style.width = "220px"; 
 
    document.getElementById("Content").style.paddingLeft = "0px"; 
 
    document.body.style.opacity = "0.1"; 
 

 
}
/* The side navigation menu */ 
 

 
.sidenav { 
 
    height: 100%; 
 
    width: 0; 
 
    /* 0 width - change this with JavaScript */ 
 
    position: fixed; 
 
    z-index: 400; 
 
    right: 0; 
 
    background-color: #0F0F0F; 
 
    overflow-x: hidden; 
 
    padding-top: 90px; 
 
    transition: 0.5s; 
 
    -webkit-transition: 0.5s; 
 
    -moz-transition: 0.5s; 
 
    opacity: 0.8; 
 
} 
 
/* The navigation menu links */ 
 

 
.sidenav a { 
 
    padding: 8px 8px 8px 10px; 
 
    text-decoration: none; 
 
    font-size: 18px; 
 
    text-align: right; 
 
    vertical-align: middle; 
 
    justify-content: center; 
 
    display: flex; 
 
    line-height: 20px; 
 
    color: #FFFFFF; 
 
    transition: 0.3s; 
 
} 
 
/* When you mouse over the navigation links, change their color */ 
 

 
.sidenav a:hover, 
 
.offcanvas a:focus { 
 
    color: #00CB10; 
 
    text-decoration: underline; 
 
} 
 
.menu-icon { 
 
    display: inline-block; 
 
    position: fixed; 
 
    z-index: 500; 
 
    cursor: pointer; 
 
    margin-right: 15px; 
 
    margin-top: 15px; 
 
    margin-left: 96%; 
 
} 
 
.bar1, 
 
.bar2, 
 
.bar3 { 
 
    width: 30px; 
 
    height: 4px; 
 
    background-color: #0F0F0F; 
 
    border-radius: 10px; 
 
    margin: 6px 0; 
 
    transition: 0.4s; 
 
} 
 
/* Rotate first bar */ 
 

 
.change .bar1 { 
 
    -webkit-transform: rotate(-45deg) translate(-9px, 5px); 
 
    transform: rotate(-45deg) translate(-9px, 5px); 
 
} 
 
/* Fade out the second bar */ 
 

 
.change .bar2 { 
 
    opacity: 0; 
 
} 
 
/* Rotate last bar */ 
 

 
.change .bar3 { 
 
    -webkit-transform: rotate(45deg) translate(-8px, -6px); 
 
    transform: rotate(45deg) translate(-8px, -6px); 
 
}
<div id="Menu" class="menu-icon" onclick="myFunction(this)"> 
 
    <div class="bar1"></div> 
 
    <div class="bar2"></div> 
 
    <div class="bar3"></div> 
 
</div> 
 

 
<div id="mySidenav" class="sidenav"> 
 
    <!--Start Side Nav--> 
 
    <a href="#">Our Story</a> 
 
    <a href="#">Products</a> 
 
    <a href="#">Contact Us</a> 
 
    <a href="#">Login/Sign up</a> 
 
</div> 
 
<!---->

回答

1

你並不需要設置寬度JS。當菜單打開時,您正在切換一個額外的課程,您可以玩這個。另一點是,你不應該改變身體的不透明度(它也會影響菜單),但只有主要的內容分區。 或者顯示具有固定位置(100%寬度和高度,背景和不透明度)的疊加div,以創建禁用內容的效果。

在我爲例我評論了不必要的JS線,和我說索姆CSS:

function myFunction(x) { 
 
    x.classList.toggle("change"); 
 
    //document.getElementById("mySidenav").style.width = "220px"; 
 
    //document.getElementById("Content").style.paddingLeft = "0px"; 
 
    document.body.style.opacity = "0.1"; 
 

 
}
/* The side navigation menu */ 
 

 
.sidenav { 
 
    height: 100%; 
 
    width: 0; 
 
    /* 0 width - change this with JavaScript */ 
 
    position: fixed; 
 
    z-index: 400; 
 
    right: 0; 
 
    background-color: #0F0F0F; 
 
    overflow-x: hidden; 
 
    padding-top: 90px; 
 
    transition: 0.5s; 
 
    -webkit-transition: 0.5s; 
 
    -moz-transition: 0.5s; 
 
    opacity: 0.8; 
 
} 
 
/* The navigation menu links */ 
 

 
.sidenav a { 
 
    padding: 8px 8px 8px 10px; 
 
    text-decoration: none; 
 
    font-size: 18px; 
 
    text-align: right; 
 
    vertical-align: middle; 
 
    justify-content: center; 
 
    display: flex; 
 
    line-height: 20px; 
 
    color: #FFFFFF; 
 
    transition: 0.3s; 
 
} 
 
/* When you mouse over the navigation links, change their color */ 
 

 
.sidenav a:hover, 
 
.offcanvas a:focus { 
 
    color: #00CB10; 
 
    text-decoration: underline; 
 
} 
 
.menu-icon { 
 
    display: inline-block; 
 
    position: fixed; 
 
    z-index: 500; 
 
    cursor: pointer; 
 
    margin-right: 15px; 
 
    margin-top: 15px; 
 
    margin-left: 96%; 
 
} 
 
.bar1, 
 
.bar2, 
 
.bar3 { 
 
    width: 30px; 
 
    height: 4px; 
 
    background-color: #0F0F0F; 
 
    border-radius: 10px; 
 
    margin: 6px 0; 
 
    transition: 0.4s; 
 
} 
 
/* Rotate first bar */ 
 

 
.change .bar1 { 
 
    -webkit-transform: rotate(-45deg) translate(-9px, 5px); 
 
    transform: rotate(-45deg) translate(-9px, 5px); 
 
} 
 
/* Fade out the second bar */ 
 

 
.change .bar2 { 
 
    opacity: 0; 
 
} 
 
/* Rotate last bar */ 
 

 
.change .bar3 { 
 
    -webkit-transform: rotate(45deg) translate(-8px, -6px); 
 
    transform: rotate(45deg) translate(-8px, -6px); 
 
} 
 
.change + .sidenav { 
 
    width: 220px; 
 
}
<div id="Menu" class="menu-icon" onclick="myFunction(this)"> 
 
    <div class="bar1"></div> 
 
    <div class="bar2"></div> 
 
    <div class="bar3"></div> 
 
</div> 
 

 
<div id="mySidenav" class="sidenav"> 
 
    <!--Start Side Nav--> 
 
    <a href="#">Our Story</a> 
 
    <a href="#">Products</a> 
 
    <a href="#">Contact Us</a> 
 
    <a href="#">Login/Sign up</a> 
 
</div> 
 
<!---->

+0

我不知道它是觸發一個額外的類。我實際上認爲我需要一個額外的onclick函數來關閉導航,但是這個工作完全謝謝你。所以基本上.change + .sidenav切換寬度切換? – Frana

+0

'x.classList.toggle(「change」);'正在切換元素'x'上的類更改(這裏是#Menu)。 'change'類是用來改變菜單按鈕形狀的類。所以我使用這個類來改變菜單寬度。 – Cladiuss

+0

CSS中的'.change + .sidenav'意味着它將搜索類'sidenav'的元素,緊跟在具有類「change」的元素之後。由於'change'只會在打開菜單時出現,CSS效果只會在打開時纔會應用 – Cladiuss

0

如果你想在JavaScript設置寬度,那麼你可以去檢查菜單是否打開。

但是,在這種情況下,上述答案更合適。

function myFunction(x) { 
 
    x.classList.toggle("change"); 
 
    var width=document.getElementById("mySidenav").style.width; 
 
    if(width!="220px" || width=="") 
 
    { 
 
    document.getElementById("mySidenav").style.width="220px"; 
 
    } 
 
    else 
 
    { 
 
    document.getElementById("mySidenav").style.width="0px"; 
 
    } 
 
    document.getElementById("Content").style.paddingLeft = "0px"; 
 
    document.body.style.opacity = "0.1"; 
 

 
}
/* The side navigation menu */ 
 

 
.sidenav { 
 
    height: 100%; 
 
    width: 0; 
 
    /* 0 width - change this with JavaScript */ 
 
    position: fixed; 
 
    z-index: 400; 
 
    right: 0; 
 
    background-color: #0F0F0F; 
 
    overflow-x: hidden; 
 
    padding-top: 90px; 
 
    transition: 0.5s; 
 
    -webkit-transition: 0.5s; 
 
    -moz-transition: 0.5s; 
 
    opacity: 0.8; 
 
} 
 
/* The navigation menu links */ 
 

 
.sidenav a { 
 
    padding: 8px 8px 8px 10px; 
 
    text-decoration: none; 
 
    font-size: 18px; 
 
    text-align: right; 
 
    vertical-align: middle; 
 
    justify-content: center; 
 
    display: flex; 
 
    line-height: 20px; 
 
    color: #FFFFFF; 
 
    transition: 0.3s; 
 
} 
 
/* When you mouse over the navigation links, change their color */ 
 

 
.sidenav a:hover, 
 
.offcanvas a:focus { 
 
    color: #00CB10; 
 
    text-decoration: underline; 
 
} 
 
.menu-icon { 
 
    display: inline-block; 
 
    position: fixed; 
 
    z-index: 500; 
 
    cursor: pointer; 
 
    margin-right: 15px; 
 
    margin-top: 15px; 
 
    margin-left: 96%; 
 
} 
 
.bar1, 
 
.bar2, 
 
.bar3 { 
 
    width: 30px; 
 
    height: 4px; 
 
    background-color: #0F0F0F; 
 
    border-radius: 10px; 
 
    margin: 6px 0; 
 
    transition: 0.4s; 
 
} 
 
/* Rotate first bar */ 
 

 
.change .bar1 { 
 
    -webkit-transform: rotate(-45deg) translate(-9px, 5px); 
 
    transform: rotate(-45deg) translate(-9px, 5px); 
 
} 
 
/* Fade out the second bar */ 
 

 
.change .bar2 { 
 
    opacity: 0; 
 
} 
 
/* Rotate last bar */ 
 

 
.change .bar3 { 
 
    -webkit-transform: rotate(45deg) translate(-8px, -6px); 
 
    transform: rotate(45deg) translate(-8px, -6px); 
 
}
<div id="Menu" class="menu-icon" onclick="myFunction(this)"> 
 
    <div class="bar1"></div> 
 
    <div class="bar2"></div> 
 
    <div class="bar3"></div> 
 
</div> 
 

 
<div id="mySidenav" class="sidenav"> 
 
    <!--Start Side Nav--> 
 
    <a href="#">Our Story</a> 
 
    <a href="#">Products</a> 
 
    <a href="#">Contact Us</a> 
 
    <a href="#">Login/Sign up</a> 
 
</div> 
 
<!---->

0

從您所設定的最高缺少的關鍵在於#mySidenav需要能夠以國家之間切換以及.menu-icon

因此,您需要設置myFunction()使其切換上.menu-icon#mySidenav.change

function myFunction() { 
 
var x = document.getElementsByClassName('menu-icon')[0]; 
 
var mySidenav = document.getElementById('mySidenav'); 
 

 
x.classList.toggle('change'); 
 
mySidenav.classList.toggle('change'); 
 
} 
 

 
var menuIcon = document.getElementsByClassName('menu-icon')[0]; 
 
menuIcon.addEventListener('click',myFunction,false);
/* The side navigation menu */ 
 

 
.sidenav { 
 
height: 100%; 
 
width: 0; 
 
/* 0 width - change this with JavaScript */ 
 
position: fixed; 
 
z-index: 400; 
 
right: 0; 
 
background-color: #0F0F0F; 
 
overflow-x: hidden; 
 
padding-top: 90px; 
 
transition: 0.5s; 
 
-webkit-transition: 0.5s; 
 
-moz-transition: 0.5s; 
 
opacity: 0.8; 
 
} 
 
/* The navigation menu links */ 
 

 
.sidenav a { 
 
padding: 8px 8px 8px 10px; 
 
text-decoration: none; 
 
font-size: 18px; 
 
text-align: right; 
 
vertical-align: middle; 
 
justify-content: center; 
 
display: flex; 
 
line-height: 20px; 
 
color: #FFFFFF; 
 
transition: 0.3s; 
 
} 
 
/* When you mouse over the navigation links, change their color */ 
 

 
.sidenav a:hover, 
 
.offcanvas a:focus { 
 
color: #00CB10; 
 
text-decoration: underline; 
 
} 
 
.menu-icon { 
 
display: inline-block; 
 
position: fixed; 
 
z-index: 500; 
 
cursor: pointer; 
 
margin-right: 15px; 
 
margin-top: 15px; 
 
margin-left: 96%; 
 
} 
 
.bar1, 
 
.bar2, 
 
.bar3 { 
 
width: 30px; 
 
height: 4px; 
 
background-color: #0F0F0F; 
 
border-radius: 10px; 
 
margin: 6px 0; 
 
transition: 0.4s; 
 
} 
 
/* Rotate first bar */ 
 

 
.change .bar1 { 
 
-webkit-transform: rotate(-45deg) translate(-9px, 5px); 
 
transform: rotate(-45deg) translate(-9px, 5px); 
 
background-color: rgb(255,255,255); 
 
} 
 
/* Fade out the second bar */ 
 

 
.change .bar2 { 
 
opacity: 0; 
 
} 
 
/* Rotate last bar */ 
 

 
.change .bar3 { 
 
-webkit-transform: rotate(45deg) translate(-8px, -6px); 
 
transform: rotate(45deg) translate(-8px, -6px); 
 
background-color: rgb(255,255,255); 
 
} 
 

 
#mySidenav.change { 
 
width: 220px; 
 
} 
 

 
#Content.change { 
 
padding-left: 0; 
 
} 
 

 
body.change { 
 
opacity: 0.1; 
 
}
<div id="Menu" class="menu-icon"> 
 
<div class="bar1"></div> 
 
<div class="bar2"></div> 
 
<div class="bar3"></div> 
 
</div> 
 

 
<div id="mySidenav" class="sidenav"> 
 
<!--Start Side Nav--> 
 
<a href="#">Our Story</a> 
 
<a href="#">Products</a> 
 
<a href="#">Contact Us</a> 
 
<a href="#">Login/Sign up</a> 
 
</div>

1

我已經添加了新的class稱爲toggleClass,我已經提供width:220px,和我有評論document.getElementById("mySidenav").style.width = "220px"

working fiddle