2016-09-21 147 views
1

我正在努力賺取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">&#9776;</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">&#9776;</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的回答在這裏。

我在這裏做錯了什麼?提前致謝。

回答

1

這裏的問題在於menu.style.height屬性未初始化爲"0px",但是""。修改你的邏輯,包括這種情況下,使切換按預期方式工作:

var hamburger = document.getElementById("burger"), 
 
menu = document.getElementById("myMenu"); 
 

 

 
hamburger.addEventListener("click",function(e){ 
 
    if (menu.style.height==="0px" || menu.style.height===""){ 
 
    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">&#9776;</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>

+0

感謝您的回覆。你的解決方案應該解決問題看,我認爲它是在我在CSS中定義它時初始化的。那是因爲它沒有被使用,直到它被命名或訪問?作爲設計師,這種事情讓我感到困惑。 –

2

菜單的初始高度爲,計算結果爲爲零,但內聯樣式的height屬性的初始值爲空字符串(因爲您尚未設置它)。

所以,當你測試:

if (menu.style.height==="0px"){ 

第一次,你打else並明確將其設置爲0px

解決此問題的最簡單方法是測試並添加和刪除類名稱而不是直接操作內聯樣式。

+0

感謝您的consise答案。這是我經常遇到的問題。那麼爲什麼它不會設置爲「0」,因爲它在CSS中被定義爲這樣?我想它不存在,直到你問它的價值與Javascript?在這裏感到困惑... –

+0

@jamesBurns - 因爲style。*屬性映射到* inline style *(即style屬性)而不是計算的樣式。 – Quentin

+0

這是有道理的。謝謝。 –

相關問題