2017-04-12 95 views
0

我很想了解JavaScript,我似乎無法弄清楚如何編寫漢堡菜單來打開並在移動視圖中顯示我的導航。我可以在桌面上獲得我的主導航,並在這些視圖中顯示我想要的移動設備和平板電腦的外觀,但該按鈕不起作用。我嘗試了一些JavaScript,但沒有成功。我會把所有內容放在一個片段中,以便有人可以幫助我理解這一點。謝謝!響應式導航瓦特/漢堡菜單

$('.toggle').click(function() { 
 
    "use strict"; 
 
    $('nav ul').slideToggle(); 
 
}); 
 

 

 

 
$(window).resize(function() { 
 
    "use strict"; 
 
    if ($(window).width() > 780) { 
 
     $('nav ul').removeAttr('style'); 
 
    } 
 
});
/* Start global */ 
 

 
* { 
 
    padding: 0; 
 
    margin: 0; 
 
    -webkit-box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    box-sizing: border-box; 
 
    text-decoration: none; 
 
    font-family: arial, tahoma; 
 
    list-style: none; 
 
    /* outline: 1px solid brown;*/ 
 
} 
 

 
/* End global */ 
 

 

 

 
header { 
 
    background-color: #333; 
 
    height: 500px; 
 
} 
 

 
/* Start navigation bar */ 
 

 
nav { 
 
    height: 80px; 
 
    background-color: #222; 
 
    border-bottom: 1px solid #555 
 
} 
 
.logo { 
 
    padding: 10px; 
 
    width: auto; 
 
    float: left; 
 
} 
 
.logo img { 
 
    height: 55px; 
 
} 
 
.list-item { 
 
    float: right; 
 
    margin-right: 25px; 
 
    margin-top: 17px; 
 
} 
 
.list-item li { 
 
    float: left; 
 
    padding: 13px 13px; 
 
    font-size: 18px; 
 
    border-radius: 3px; 
 
    transition: all .7s ease-in-out; 
 
} 
 
.list-item li a { 
 
    color: #EEE; 
 
} 
 
.list-item li:hover { 
 
    background-color: brown; 
 
} 
 

 
/* End navigation bar */ 
 

 

 

 

 

 
/* Start nav bar for small screens */ 
 

 
.icon { 
 
    display: none; 
 
} 
 
.toggle { 
 
    float: right; 
 
    margin: 20px; 
 
    color: #EEE; 
 
    font-size: 30px; 
 
    border: 1px solid #EEE; 
 
    padding: 0px 5px; 
 
    border-radius: 4px; 
 
    cursor: pointer; 
 
} 
 

 
/* End nav bar for small screens */ 
 

 
/* Start media query */ 
 

 
@media (max-width: 775px) { 
 
    .icon { 
 
    display: block; 
 
    width: 100%; 
 
    height: 80px; 
 
    background-color: #111; 
 
    border-bottom: 1px solid #444; 
 
    } 
 
    .list-item { 
 
    width: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
    position: relative; 
 
    top: -4px; 
 
    background-color: #222; 
 
    display: none; 
 
    } 
 
    .list-item li { 
 
    text-align: center; 
 
    display: block; 
 
    border-bottom: 1px solid #333; 
 
    float: none; 
 
    } 
 
\t 
 
\t .result-iframe { 
 
    border: 0; 
 
    background: white; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 1; 
 
} 
 

 

 
/* End media query */ 
 

 
}
<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<meta charset="UTF-8"> 
 
<title>Untitled Document</title> 
 

 
<link href="about.css" rel="stylesheet"> 
 
<link href="about.js" rel="alternate"> 
 

 

 
</head> 
 

 

 
<body> 
 

 

 

 
<header> 
 
     <nav> 
 
     <div class="logo"> 
 
      <img src="https://s-media-cache-ak0.pinimg.com/736x/ec/13/a7/ec13a753972c254761be4d9d5666d341.jpg" /> 
 
     </div> 
 
     <div class="icon"> 
 
      <span class="toggle">☰</span> 
 
     </div> 
 
     <ul class="list-item"> 
 
      <li><a href="#">Home</a></li> 
 
      <li><a href="#">Facebook</a></li> 
 
      <li><a href="#">Twitter</a></li> 
 
      <li><a href="#">Categorys</a></li> 
 
      <li><a href="#">Contact us</a></li> 
 
      <li><a href="#">About us</a></li> 
 
     </ul> 
 
     </nav> 
 
    </header> 
 

 

 
</body> 
 
</html>

+0

你在哪裏鏈接到您的jQuery文件/ CDN? – Mark

+0

在我的頭部分:我可以編輯該片段,以便包含該片段。我很抱歉。 @MadGab – Cakers

+1

記住,您需要在DOM加載後啓動JavaScript代碼。你可以把你的javascript放在body的最後,或者使用$(document).ready(function(){<你的javascript或初始化函數在這裏>}); – alfredo

回答

1

我添加jQuery,它似乎是工作的罰款。

$('.toggle').click(function() { 
 
    "use strict"; 
 
    $('nav ul').slideToggle(); 
 
}); 
 

 

 

 
$(window).resize(function() { 
 
    "use strict"; 
 
    if ($(window).width() > 780) { 
 
     $('nav ul').removeAttr('style'); 
 
    } 
 
});
/* Start global */ 
 

 
* { 
 
    padding: 0; 
 
    margin: 0; 
 
    -webkit-box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    box-sizing: border-box; 
 
    text-decoration: none; 
 
    font-family: arial, tahoma; 
 
    list-style: none; 
 
    /* outline: 1px solid brown;*/ 
 
} 
 

 
/* End global */ 
 

 

 

 
header { 
 
    background-color: #333; 
 
    height: 500px; 
 
} 
 

 
/* Start navigation bar */ 
 

 
nav { 
 
    height: 80px; 
 
    background-color: #222; 
 
    border-bottom: 1px solid #555 
 
} 
 
.logo { 
 
    padding: 10px; 
 
    width: auto; 
 
    float: left; 
 
} 
 
.logo img { 
 
    height: 55px; 
 
} 
 
.list-item { 
 
    float: right; 
 
    margin-right: 25px; 
 
    margin-top: 17px; 
 
} 
 
.list-item li { 
 
    float: left; 
 
    padding: 13px 13px; 
 
    font-size: 18px; 
 
    border-radius: 3px; 
 
    transition: all .7s ease-in-out; 
 
} 
 
.list-item li a { 
 
    color: #EEE; 
 
} 
 
.list-item li:hover { 
 
    background-color: brown; 
 
} 
 

 
/* End navigation bar */ 
 

 

 

 

 

 
/* Start nav bar for small screens */ 
 

 
.icon { 
 
    display: none; 
 
} 
 
.toggle { 
 
    float: right; 
 
    margin: 20px; 
 
    color: #EEE; 
 
    font-size: 30px; 
 
    border: 1px solid #EEE; 
 
    padding: 0px 5px; 
 
    border-radius: 4px; 
 
    cursor: pointer; 
 
} 
 

 
/* End nav bar for small screens */ 
 

 
/* Start media query */ 
 

 
@media (max-width: 775px) { 
 
    .icon { 
 
    display: block; 
 
    width: 100%; 
 
    height: 80px; 
 
    background-color: #111; 
 
    border-bottom: 1px solid #444; 
 
    } 
 
    .list-item { 
 
    width: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
    position: relative; 
 
    top: -4px; 
 
    background-color: #222; 
 
    display: none; 
 
    } 
 
    .list-item li { 
 
    text-align: center; 
 
    display: block; 
 
    border-bottom: 1px solid #333; 
 
    float: none; 
 
    } 
 
\t 
 
\t .result-iframe { 
 
    border: 0; 
 
    background: white; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 1; 
 
} 
 

 

 
/* End media query */ 
 

 
}
<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<meta charset="UTF-8"> 
 
<title>Untitled Document</title> 
 

 
<link href="about.css" rel="stylesheet"> 
 
<link href="about.js" rel="alternate"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
</head> 
 

 

 
<body> 
 

 

 

 
<header> 
 
     <nav> 
 
     <div class="logo"> 
 
      <img src="https://s-media-cache-ak0.pinimg.com/736x/ec/13/a7/ec13a753972c254761be4d9d5666d341.jpg" /> 
 
     </div> 
 
     <div class="icon"> 
 
      <span class="toggle">☰</span> 
 
     </div> 
 
     <ul class="list-item"> 
 
      <li><a href="#">Home</a></li> 
 
      <li><a href="#">Facebook</a></li> 
 
      <li><a href="#">Twitter</a></li> 
 
      <li><a href="#">Categorys</a></li> 
 
      <li><a href="#">Contact us</a></li> 
 
      <li><a href="#">About us</a></li> 
 
     </ul> 
 
     </nav> 
 
    </header> 
 

 

 
</body> 
 
</html>