我有一個菜單,當你點擊漢堡圖標時,我想下拉菜單。點擊按鈕後顯示列表項,但當按鈕未被點擊時仍然可以看到列表項。我無法弄清楚如何隱藏菜單。切換菜單列表項目顯示
我這裏的代碼:Codepen
function myFunction() {
\t var x = document.getElementById("myTopnav");
\t if (x.className === "topnav") {
\t x.className += " responsive";
\t } else {
\t x.className = "topnav";
\t }
\t }
$(document).ready(function() {
$('#nav-icon3').on('click', function() {
$(this).toggleClass('open');
});
});
* {
\t box-sizing: border-box;
}
html, body {
\t padding: 0;
\t margin: 0;
\t width: 100%;
\t height: 100%;
\t font-size: 1rem;
}
.header {
\t background-color: #FFF;
\t width: 100%;
\t margin: 0px auto;
\t display: block;
\t top: 0;
\t margin-top: 7%;
\t height: 80px;
}
h1 {
\t margin-left: 5%;
\t padding-left: 1%;
\t font-family: 'Lobster', cursive;
\t margin-top: 0;
\t float: left;
\t margin-bottom: 0;
\t color: #D18E8F;
}
#nav-icon {
\t width: 30px;
\t height: 30px;
\t position: relative;
\t margin-right: 7%;
\t margin-top: 8px;
\t /*padding-right: 1%;*/
\t float: right;
\t -webkit-transform: rotate(0deg);
\t -moz-transform: rotate(0deg);
\t -o-transform: rotate(0deg);
\t transform: rotate(0deg);
\t -webkit-transition: .5s ease-in-out;
\t -moz-transition: .5s ease-in-out;
\t -o-transition: .5s ease-in-out;
\t cursor: pointer;
}
#nav-icon3 span {
\t display: block;
\t position: absolute;
\t height: 4px;
\t width: 100%;
\t background: #8c8c8c;
\t border-radius: 6px;
\t opacity: 1;
\t left: 0;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .25s ease-in-out;
-moz-transition: .25s ease-in-out;
-o-transition: .25s ease-in-out;
transition: .25s ease-in-out;
}
#nav-icon3 span:nth-child(1) {
\t top: 0px;
}
#nav-icon3 span:nth-child(2), #nav-icon3 span:nth-child(3) {
\t top: 10px;
}
#nav-icon3 span:nth-child(4) {
\t top: 20px;
}
#nav-icon3.open span:nth-child(1) {
\t top: 10px;
\t width: 0%;
\t left: 50%;
}
#nav-icon3.open span:nth-child(2) {
\t -webkit-transform: rotate(45deg);
\t -moz-transform: rotate(45deg);
\t -o-transform: rotate(45deg);
\t transform: rotate(45deg);
}
#nav-icon3.open span:nth-child(3) {
\t -webkit-transform: rotate(-45deg);
\t -moz-transform: rotate(-45deg);
\t -o-transform: rotate(-45deg);
\t transform: rotate(-45deg);
}
#nav-icon3.open span:nth-child(4) {
\t top: 10px;
\t width: 0%;
\t left: 50%;
}
.topnav.responsive {
\t /*display: none;*/
\t clear: both;
\t padding: 0;
\t height: 160px;
}
ul.topnav.responsive {
\t list-style: none;
\t font-family: 'Raleway', 'sans-serif';
\t float: left;
\t margin-left: 7%;
\t /*padding-left: 2%;*/
\t margin-top: 0;
\t width: 85%;
\t margin-bottom: 0;
}
ul.topnav.responsive li {
\t padding-top: 16px;
\t padding-bottom: 16px;
\t border-bottom: 1px solid #c6c6c6;
}
ul.topnav.responsive li a {
\t text-decoration: none;
\t color: #8c8c8c;
}
ul.topnav.responsive li a:hover {
\t color: #7EBEA3;
}
<!DOCTYPE html>
<html>
\t <head>
\t \t <meta charset="UTF-8">
\t \t <meta name="viewport" content="width=device-width, initial scale=1.0">
\t \t <title>Gabriella Farfan</title>
\t \t <link href="https://fonts.googleapis.com/css?family=Lobster|Raleway" rel="stylesheet">
\t \t <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
\t \t <link rel="stylesheet" href="http://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css">
\t \t <link rel="stylesheet" type="text/css" href="css/style.css">
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
\t \t <script src="js/main.js"></script>
\t </head>
<body>
<div class="header">
\t \t <h1>Gabriella Farfan</h1>
\t \t \t <div id="nav-icon">
\t \t \t \t <div id="nav-icon3">
\t \t \t \t \t <a id="nav-icon3" onclick="myFunction();">
\t \t \t \t \t \t <span></span>
\t \t \t \t \t \t <span></span>
\t \t \t \t \t \t <span></span>
\t \t \t \t \t \t <span></span>
\t \t \t \t \t </a>
\t \t \t \t </div>
\t \t \t </div>
\t \t <ul class="topnav" id="myTopnav">
\t \t \t <li>
\t \t \t \t <a href="#">Work</a>
\t \t \t </li>
\t \t \t <li>
\t \t \t \t <a href="About.html">About Me</a>
\t \t \t </li>
\t \t \t <li style="border-bottom: none">
\t \t \t \t <a href="#">Contact</a>
\t \t \t </li>
\t \t </ul>
\t </div>
</body>
</html>
太感謝你了,這解決了我的問題 – user7311741