【发布时间】:2020-04-11 23:32:08
【问题描述】:
我正在尝试使用 jQuery 或 JS 做一个简单的汉堡菜单(无论哪种方式我都很简单),但我的代码根本无法工作,而且我终其一生都无法找出原因。
它的结构是左上角的汉堡包,中间的徽标,然后是个人资料和右上角的篮子。
目前我要做的就是让导航列表在点击时出现,然后我可以设置它的样式。我正在尝试使用 .hidden 类在单击汉堡时将导航显示为块。
代码如下:
HTML:
<section class="header-section">
<div class="header-container">
<div class="header-content">
<div class="hamburger-container">
<i id="burger" class="fas fa-bars fa-2x"></i>
</div>
<div class="header-logo-container">
<h2>Logo goes here</h2>
</div>
<div class="header-profile-and-signin">
<i class="fas fa-user-circle fa-2x"></i>
<i class="fas fa-suitcase-rolling fa-2x"></i>
</div>
<ul id="nav">
<li>
<a href="search-page.html" style="text-decoration: none">Holidays</a>
</li>
<li>
<a href="sign-in.html" style="text-decoration: none">Sign In / Register</a>
</li>
</ul>
</div>
</div>
</section>
CSS:
body {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header-section {
height: 100px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: #999;
}
.header-container {
height: 100%;
width: 90%;
display: flex;
align-items: center;
justify-content: center;
background-color: blue;
}
.header-content {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: yellow;
}
.hamburger-container {
height: 100%;
width: 33.33%;
display: flex;
align-items: center;
background-color: red;
}
.hamburger-container i {
margin-left: 20px;
}
.header-logo-container {
height: 100%;
width: 33.33%;
display: flex;
align-items: center;
justify-content: center;
background-color: green;
}
.header-profile-and-signin {
height: 100%;
width: 33.33%;
display: flex;
align-items: center;
justify-content: space-around;
}
#nav {
list-style: none;
display: none;
}
.hidden {
display: block;
}
#burger {
cursor: pointer;
}
jQuery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$("burger").click(function() {
$("nav").toggleClass("hidden");
});
</script>
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: javascript jquery html css hamburger-menu