【发布时间】:2021-10-06 19:35:04
【问题描述】:
我的网站上有一个汉堡菜单,它只显示在 600 像素或以下。我希望我的类别位于此图标下。这将需要使这些菜单项在 600px 阈值处从绿色导航栏消失,然后重新出现在该汉堡包下。实现这一目标的最佳方法是什么?
Here is the live view of the site 这是我的代码:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #47a23f;
}
.topnav a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.topnav a:hover, .dropdown:hover .dropbtn {
background-color: #005da6;
}
.topnav a.active {
background-color: #005da6;
color: white;
}
.topnav .icon {
display: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #47a23f;
min-width: 13%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color:#fff;
padding: 5% 10%;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #003150;
color:#fff;
}
.dropdown:hover .dropdown-content {
display: block;
color:#fff;
}
.topnav > .active {
background-color: #005da6;
}
.topnav:hover > .active {
background-color: inherit;
}
.topnav > .active:hover {
background-color: #005da6;
}
.dropdown-content > .active {
background-color: #003150;
}
.dropdown-content:hover > .active {
background-color: inherit;
}
.dropdown-content > .active:hover {
background-color: #003150;
}
@media screen and (max-width: 600px) {
/* .topnav a:not(:first-child) {display: none;} */
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
</style>
<div class="topnav" id="myTopnav">
<a href="https://cloud.e.activehealth.com/BYBO/Home" class="active">Home</a>
<div class="dropdown">
<button class="dropbtn">Workforce
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a target="blank" href="https://myactivehealth.com/Portal/PreRegistrations/Index">Request to enroll</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/Registration/RegistrationStep1?IsoAuthDPRequest=False">Create an account</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/PortalLogin.aspx?SupplierURL=15571">Log in</a>
</div>
</div>
<div class="dropdown">
<a href="https://cloud.e.activehealth.com/BYBO/Employer">Employer</a></div>
<div class="dropdown">
<button class="dropbtn">Injured Workers
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a target="blank" href="https://www.myactivehealth.com/Portal/Registration/RegistrationStep1?IsoAuthDPRequest=False">Create an account</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/PortalLogin.aspx?SupplierURL=15571">Log in</a>
</div>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
【问题讨论】:
-
1) 汉堡菜单必须切换整个导航栏(从显示无到显示块或弹性,反之亦然)。 2)您必须重新设置导航项/链接的样式,使其符合您的喜好,低于 600 像素阈值。
-
这是我如何通过 Chrome 开发工具使用 flex 设置样式的示例:gyazo.com/25e2cd32bc1bcbf37f630f5471373bf6
标签: javascript html css hamburger-menu