javascript - 带有下拉菜单的响应式导航栏不起作用
问题描述
我正在创建一个响应式导航栏,其中包含一个下拉菜单。下面是我的代码:
我有内联的 CSS 代码和媒体查询用于测试目的。所以它可能看起来很长。
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="../../themes/bootstrap/starterkits/THEMENAME/css/style.css" rel="stylesheet" />
<link href="//fonts.googleapis.com/css?family=Raleway" rel="stylesheet" type="text/css" />
<style type="text/css">body {margin:0;font-family:Arial}
.topnav {
overflow: hidden;
background-color: #ffffff;
}
.home {
float: left;
overflow: hidden;
}
.topnav a {
float: right;
display: block;
color: #000000;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
.dropdown {
float: right;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
padding: 14px 16px;
background-color: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover, .dropdown:hover .dropbtn {
background-color: #555;
color: #D5DBDB;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child), .dropdown .dropbtn {
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;
}
.topnav.responsive .dropdown {float: none;}
.topnav.responsive .dropdown-content {position: relative;}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
</style>
<div class="panel">
<div class="topnav" id="myTopnav">
<div class="home"><a href="#"><img src="../../sites/default/files/logo_0.png" /></a></div>
<div class="dropdown">
<div class="dropbtn"><a href="#">Programs</a></div>
<div class="dropdown-content"><a href="#">Success</a></div>
</div>
<a href="#">Contact</a>
<a href="#">Families</a>
<a href="#">About</a>
<a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">☰</a></div>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
该菜单包含左侧的主页徽标图像和右侧的 4 个菜单项。
在活动的“主页”链接之后是“程序”下拉按钮,其下方应有一个“成功”下拉菜单。此下拉列表未正确加载,并且设计失真。有什么帮助解决这个问题吗?
解决方案
我建议不要使用浮动进行布局。Flexbox 在这方面要好得多,并且如今在浏览器中得到广泛支持。
浮动打破了你的 html 元素的流动。
此外,.dropbtn 上有一个填充,这使得这些项目不符合要求。
body {
margin:0;
font-family:Arial
}
.topnav {
display: flex;
justify-content: flex-end;
overflow: hidden;
background-color: #ffffff;
}
.home {
margin-right: auto;
}
.topnav a {
display: block;
color: #000000;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
.dropdown {
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
background-color: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover, .dropdown:hover .dropbtn {
background-color: #555;
color: #D5DBDB;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child), .dropdown .dropbtn {
display: none;
}
.topnav a.icon {
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
display: block;
text-align: left;
}
.topnav.responsive .dropdown {float: none;}
.topnav.responsive .dropdown-content {position: relative;}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
<!doctype html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="../../themes/bootstrap/starterkits/THEMENAME/css/style.css" rel="stylesheet" />
<link href="//fonts.googleapis.com/css?family=Raleway" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="panel">
<div class="topnav" id="myTopnav">
<div class="home">
<a href="#">
<img src="../../sites/default/files/logo_0.png" />
</a>
</div>
<a href="#">About</a>
<a href="#">Families</a>
<a href="#">Contact</a>
<div class="dropdown">
<div class="dropbtn">
<a href="#">Programs</a>
</div>
<div class="dropdown-content">
<a href="#">Success</a>
</div>
</div>
<a class="icon" href="javascript:void(0);" onclick="myFunction()" style="font-size:15px;">☰</a>
</div>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
推荐阅读
- python - 如何通过 for 循环中的 for 循环将列表附加到数据帧
- chapel - Chapel LinearAlgebra 在调用 eig(csrA, left = true, right = true); 时报告错误
- javascript - 如何在 jquery 中获取数据库信息?
- ios - 从自定义视图加载表格单元内容
- regex - 调整正则表达式以调整电子邮件地址但保留某些用户名
- python - 将包含字符串数组的 .mat 文件加载到 Python 3.6
- ios - Xcode 11 每次运行前都会将应用重新安装到设备上
- mysql - 如何使列在其他列中具有唯一值
- r - 根据R中另一列中的值范围按列值选择行
- r - 从长格式转换为具有多个唯一变量的宽格式到 R 中的其他唯一变量