我正在努力賺取Javascript,特別是如何添加onclick事件和操作DOM。需要2次點擊才能激活按鈕
我在Codepen上設置了一個真實世界的例子。
擠你的瀏覽器,你會得到頂部的經典「漢堡包」菜單。這工作正常,除了(刷新頁面後)我必須點擊漢堡兩次才能激活它。
下面的代碼:
var hamburger = document.getElementById("burger"),
menu = document.getElementById("myMenu");
hamburger.addEventListener("click",function(e){
if (menu.style.height==="0px"){
menu.style.height="480px";
hamburger.style.transform="rotate(90deg)";
}else{
menu.style.height="0px";
hamburger.style.transform="rotate(0deg)";
}
});
body {
font: 13pt/130% 'Linden Hill', Times, 'Times New Roman', serif;
background: #BEDFFC;
margin: 0;
}
#burger {
display: none;
color: #BEDFFC;
font-size: 2em;
line-height: 1.25em;
position: relative;
transition: all .25s;
height: 1em;
width: 1em;
left:20px;
top:10px;
transform: rotate(45deg);
}
nav {
width: 100%;
margin: 0 auto;
}
nav ul {
list-style: none;
}
nav a {
font: .9em 'Archivo Narrow', "Helvetica Neue", sans-serif;
display: block;
float: left;
text-align:center;
text-decoration: none;
width: 12%;
margin-right: 1%;
color: rgba(122, 166, 216, 0.87);
border: 1px solid rgba(220, 247, 253, 0.55);
border-radius: 0px 10px 0px 0px;
transition: all .5s ease;
}
nav a.current,
nav a:hover {
color: #dbebf7;
text-decoration: none;
background: #539fd9;
}
@media only screen and (max-width: 800px) {
nav {
background: #265980;
width: 100vw;
height: 60px;
transition: all.5s;
}
nav a {
float: none;
border-radius: 0;
background: hsla(209, 78%, 93%,.75);
width:100%;
height: 40px;
font-size: 30px;
}
#myMenu {
width: 75%;
height: 0px;
margin: 18px auto;
padding: 0;
transition: all .25s;
overflow: hidden;
}
#burger {
display: initial;
}
#burger:hover{
color:white;
}
}
<nav id="myNav">
<div id="burger">☰</div>
<ul id="myMenu">
<li><a href="#" class="current">menu 1</a></li>
<li><a href="#" >menu 2</a></li>
<li><a href="#">menu 3</a></li>
<li><a href="#" title="interactive">menu 4</a></li>
<li><a href="#">menu 5</a></li>
<li><a href="#">menu 6</a></li>
\t <li><a href="#">menu 7</a></li>
</ul>
</nav>
現在,如果我刪除的條件在的addEventListener塊,只是改變元素的樣式,它工作正常。
var hamburger = document.getElementById("burger"),
menu = document.getElementById("myMenu");
//works, but doesn't toggle, of course...
hamburger.addEventListener("click",function(e){
menu.style.height="480px";
});
body {
font: 13pt/130% 'Linden Hill', Times, 'Times New Roman', serif;
background: #BEDFFC;
margin: 0;
}
#burger {
display: none;
color: #BEDFFC;
font-size: 2em;
line-height: 1.25em;
position: relative;
transition: all .25s;
height: 1em;
width: 1em;
left:20px;
top:10px;
transform: rotate(45deg);
}
nav {
width: 100%;
margin: 0 auto;
}
nav ul {
list-style: none;
}
nav a {
font: .9em 'Archivo Narrow', "Helvetica Neue", sans-serif;
display: block;
float: left;
text-align:center;
text-decoration: none;
width: 12%;
margin-right: 1%;
color: rgba(122, 166, 216, 0.87);
border: 1px solid rgba(220, 247, 253, 0.55);
border-radius: 0px 10px 0px 0px;
transition: all .5s ease;
}
nav a.current,
nav a:hover {
color: #dbebf7;
text-decoration: none;
background: #539fd9;
}
@media only screen and (max-width: 800px) {
nav {
background: #265980;
width: 100vw;
height: 60px;
transition: all.5s;
}
nav a {
float: none;
border-radius: 0;
background: hsla(209, 78%, 93%,.75);
width:100%;
height: 40px;
font-size: 30px;
}
#myMenu {
width: 75%;
height: 0px;
margin: 18px auto;
padding: 0;
transition: all .25s;
overflow: hidden;
}
#burger {
display: initial;
}
#burger:hover{
color:white;
}
}
<nav id="myNav">
<div id="burger">☰</div>
<ul id="myMenu">
<li><a href="#" class="current">menu 1</a></li>
<li><a href="#" >menu 2</a></li>
<li><a href="#">menu 3</a></li>
<li><a href="#" title="interactive">menu 4</a></li>
<li><a href="#">menu 5</a></li>
<li><a href="#">menu 6</a></li>
\t <li><a href="#">menu 7</a></li>
</ul>
</nav>
尋找一個純JavaScript,非jQuery的回答在這裏。
我在這裏做錯了什麼?提前致謝。
感謝您的回覆。你的解決方案應該解決問題看,我認爲它是在我在CSS中定義它時初始化的。那是因爲它沒有被使用,直到它被命名或訪問?作爲設計師,這種事情讓我感到困惑。 –