2017-07-09 252 views
0

你好,我是這個網絡編程的真正新手,剛剛在幾天前瞭解到,並嘗試做出我的響應式導航欄。我找到了w3school的教程,我遵循了它,並修改了一些代碼,但它並沒有像預期的那樣工作。菜單成功在所需的寬度上摺疊,但漢堡菜單不顯示。我想我已經改變了,並且與我的課程相匹配,但是我不知道還有什麼不對。漢堡菜單不顯示在響應式導航欄

這裏是我到目前爲止已經試過:

.navSection { 
 
    width: 100%; 
 
    display: inline-table; 
 
    line-height: 30px; 
 
    background: #1c948a; 
 
    z-index: 3; 
 
    box-shadow: 0px 3px 5px 1px rgba(0, 0, 0, .3); 
 
} 
 

 
.navMenu .icon{ 
 
    display: none; 
 
} 
 

 
@media screen and (max-width: 700px) { 
 
    .navMenu ul li:not(:first-child) {display: none;} 
 
    .navMenu ul li.icon { 
 
    float: right; 
 
    display: block; 
 
    } 
 
} 
 

 
@media screen and (max-width: 700px) { 
 
    .navMenu.responsive {position: relative;} 
 
    .navMenu.responsive .icon { 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    } 
 
    .navMenu.responsive ul li { 
 
    float: none; 
 
    display: block; 
 
    text-align: left; 
 
    } 
 
} 
 

 
.navSectionWrapper { 
 
    width: 90%; 
 
    margin: auto; 
 
} 
 

 
.homelink { 
 
    text-decoration: none; 
 
} 
 

 
.navlogo { 
 
    width: 30%; 
 
    height: 70px; 
 
    float: left; 
 
} 
 

 
.logo { 
 
    display: inline-block; 
 
    font-family: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif; 
 
    color: #2C3E50; 
 
    position: absolute; 
 
} 
 

 
.logoimg { 
 
    height: 70px; 
 
    float: left; 
 
} 
 

 
.logotext { 
 
    font-weight: 600; 
 
    float: right; 
 
    line-height: 70px; 
 
} 
 

 
.logotext>span { 
 
    color: white; 
 
    text-shadow: 2px 2px 2px #33425B; 
 
} 
 

 
.navMenu { 
 
    float: right; 
 
    text-align: center; 
 
    overflow: hidden; 
 
} 
 

 
.navMenu>ul { 
 
    list-style: none; 
 
} 
 

 
.navMenu>ul>li { 
 
    display: inline-block; 
 
    line-height: 70px; 
 
} 
 

 
.navMenu>ul>li>a>span { 
 
    color: white; 
 
    font-weight: 700; 
 
    font-size: 17px 
 
} 
 

 
.navMenu>ul>li>a { 
 
    text-decoration: none; 
 
    color: #2C3E50; 
 
    font-family: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif; 
 
    font-weight: 600; 
 
    margin: 10px 
 
} 
 

 
.navMenu>ul>li>a:hover { 
 
    color: snow; 
 
}
<div class="navSection"> 
 
     <div class="navSectionWrapper"> 
 
      <div class="navlogo"> 
 
       <a href="#" class="homelink"> 
 
        <div class="logo"> 
 
         <img src="img/logo.png" class="logoimg"> 
 
         <h2 class="logotext">Let's<span>Go</span></h2> 
 
        </div> 
 
       </a> 
 
      </div> 
 
      <div class="navMenu" id="mynavMenu"> 
 
       <ul> 
 
        <li><a href="#">Home</a></li> 
 
        <li><a href="#">Features</a></li> 
 
        <li><a href="#">About</a></li> 
 
        <li><a href="#">+<span>Download</span></a></li> 
 
        <li><a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a></li> 
 
       </ul> 
 
      </div> 
 
     </div> 
 
    </div> 
 
<script> 
 
     function myFunction() { 
 
      var x = document.getElementById("mynavMenu"); 
 
      if (x.className === "navMenu") { 
 
       x.className += " responsive"; 
 
      } else { 
 
       x.className = "navMenu"; 
 
      } 
 
     } 
 
</script>

三江源

回答

0

你的HTML使用a.icon但你在CSS與li.icon目標。將class="icon"移至li而不是a,並稍微修改了.icon的CSS。您在響應視圖中隱藏了:not(:first-child()),並且您要麼使用:not(:last-child),因爲.icon:last-child,或者只是使用:not(.icon)

.navSection { 
 
    width: 100%; 
 
    display: inline-table; 
 
    line-height: 30px; 
 
    background: #1c948a; 
 
    z-index: 3; 
 
    box-shadow: 0px 3px 5px 1px rgba(0, 0, 0, .3); 
 
} 
 

 
.navMenu .icon{ 
 
    display: none; 
 
    float: right; 
 
} 
 

 
@media screen and (max-width: 700px) { 
 
    .navMenu ul li:not(.icon) {display: none;} 
 
    .navMenu ul li.icon { 
 
    display: block; 
 
    } 
 
} 
 

 
@media screen and (max-width: 700px) { 
 
    .navMenu.responsive {position: relative;} 
 
    .navMenu.responsive .icon { 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    } 
 
    .navMenu.responsive ul li { 
 
    float: none; 
 
    display: block; 
 
    text-align: left; 
 
    } 
 
} 
 

 
.navSectionWrapper { 
 
    width: 90%; 
 
    margin: auto; 
 
} 
 

 
.homelink { 
 
    text-decoration: none; 
 
} 
 

 
.navlogo { 
 
    width: 30%; 
 
    height: 70px; 
 
    float: left; 
 
} 
 

 
.logo { 
 
    display: inline-block; 
 
    font-family: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif; 
 
    color: #2C3E50; 
 
    position: absolute; 
 
} 
 

 
.logoimg { 
 
    height: 70px; 
 
    float: left; 
 
} 
 

 
.logotext { 
 
    font-weight: 600; 
 
    float: right; 
 
    line-height: 70px; 
 
} 
 

 
.logotext>span { 
 
    color: white; 
 
    text-shadow: 2px 2px 2px #33425B; 
 
} 
 

 
.navMenu { 
 
    float: right; 
 
    text-align: center; 
 
    overflow: hidden; 
 
} 
 

 
.navMenu>ul { 
 
    list-style: none; 
 
} 
 

 
.navMenu>ul>li { 
 
    display: inline-block; 
 
    line-height: 70px; 
 
} 
 

 
.navMenu>ul>li>a>span { 
 
    color: white; 
 
    font-weight: 700; 
 
    font-size: 17px 
 
} 
 

 
.navMenu>ul>li>a { 
 
    text-decoration: none; 
 
    color: #2C3E50; 
 
    font-family: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif; 
 
    font-weight: 600; 
 
    margin: 10px 
 
} 
 

 
.navMenu>ul>li>a:hover { 
 
    color: snow; 
 
}
<div class="navSection"> 
 
     <div class="navSectionWrapper"> 
 
      <div class="navlogo"> 
 
       <a href="#" class="homelink"> 
 
        <div class="logo"> 
 
         <img src="img/logo.png" class="logoimg"> 
 
         <h2 class="logotext">Let's<span>Go</span></h2> 
 
        </div> 
 
       </a> 
 
      </div> 
 
      <div class="navMenu" id="mynavMenu"> 
 
       <ul> 
 
        <li><a href="#">Home</a></li> 
 
        <li><a href="#">Features</a></li> 
 
        <li><a href="#">About</a></li> 
 
        <li><a href="#">+<span>Download</span></a></li> 
 
        <li class="icon"><a href="javascript:void(0);" onclick="myFunction()">&#9776;</a></li> 
 
       </ul> 
 
      </div> 
 
     </div> 
 
    </div> 
 
<script> 
 
     function myFunction() { 
 
      var x = document.getElementById("mynavMenu"); 
 
      if (x.className === "navMenu") { 
 
       x.className += " responsive"; 
 
      } else { 
 
       x.className = "navMenu"; 
 
      } 
 
     } 
 
</script>

+0

謝謝!我明白了,但我對於「你的HTML使用a.icon但是你使用li.icon在CSS中定位」這部分有點不清楚。「哪裏是?爲什麼你要添加float:right to .navMenu .icon CSS?這是必要的嗎? – RadVolan

+0

@RadVolan不用客氣。 '

  • ' - 類'.icon'在'a'上,但是你的css選擇器是'.navMenu ul li.icon' - 所以你試圖把css應用到'li.icon',你的html是實際上'a.icon' - 有意義嗎? –

    +0

    是的,所以我可以使它與'a' - >'

  • '中的圖標類一起使用並使用'.navMenu a.icon'?對? – RadVolan

    相關問題