使用javascript创建垂直响应导航栏



我目前正在尝试从头开始创建一个网站,因为我有时间练习。

到目前为止,我已经有了一个可以工作的导航栏(它实际上还没有带你去其他页面,但确实可以工作(。

我决定让这个导航栏响应,因为它是一个相当大的栏。我已经给出了一个选项,点击一个按钮就可以选择竖条。需要注意的是,只有当浏览器宽度小于900px时,该按钮才对用户可用。

我目前的问题是,当按下按钮时,什么都没有显示。我已经通过试错确保了按钮的javascript正常工作,但仍然没有运气。

我是新手,所以如果我的错误是愚蠢的,请原谅我,但任何帮助都将不胜感激。

为了帮助人们了解我正在努力实现的目标,以下是我一直用作指导的链接:https://www.w3schools.com/howto/howto_js_topnav_responsive.asp

如果问题在于我使用"无序列表"标签将导航栏向右对齐,将徽标向左对齐,那么也欢迎任何其他方式!

谢谢。

p.s.忽略导航中每个部分的名称,我现在只是在填充空格^^

body{
	background-color: grey;
	margin:0;
}
/*----------------------NAVIGATION BAR----------------------*/
.nav-container{
	background-color: white;
	float: right;
	height: 80px;
	position: absolute;
	width: 100%;
	margin-top: 0;
}
#nav-menu{
	float:right;
	padding: 13px 13px;	
}
#nav-menu li{
	display:inline-block;
	font-size: 20px;
	padding: 10px 12px;
	text-align: center;
}
#nav-menu li a:not(.nav-active){	
	color: black;	
	text-decoration: none;
}
#nav-menu li a:hover:not(#logo){
	color: #0aaaa0;	
}
.nav-active {
	color: #0aaaa0;
	text-decoration: none;
}
/* LOGO */
#logo{
	padding: 0px 13px;
	float: left;
	font-size: 27px;
}
/* Hide the link that should open and close the topnav on small screens */
#nav-menu .icon {
display: none;
}
/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
@media screen and (max-width: 900px) {
#nav-menu li a:not(.icon) {display: none;}
#nav-menu li a.icon {
float: right;
display: block;
}
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 900px) {
.nav-bar.responsive {position: relative;}
.nav-bar.responsive li a.icon{
position: relative;
right: 0;
top: 0;
}
.nav-bar.responsive li a{
	float: none;
	display: block;
text-align: left;
	position: relative;
}
}
<!DOCTYPE html>
<html>
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta charset="UTF-8">
	<link rel="stylesheet" href="practice.css">
	<!-- Load an icon library to show a hamburger menu (bars) on small screens -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<!-- NAVIGATION BAR -->
<div  class="nav-container">
	<ul id="logo">Dellion</ul>
	<ul class="nav-bar" id="nav-menu">		
		<li><a class="nav-active" href="index.html">Home</a></li>
		<li><a href="cars.html">Cars</a></li>
		<li><a href="charities.html">Charities</a></li>
		<li><a href="pros.html">Pros</a></li>
		<li><a href="pros.html">Games</a></li>
		<li><a href="auctions.html">Auctions</a></li>	
		<li><a href="support.html">Support</a></li>
		<li><a href="javascript:void(0);" class="icon" onclick="myFunction()">
		<i class="fa fa-bars"></i></a></li>
	</ul>
</div>
<script>
function myFunction() {
var x = document.getElementById("nav-menu");
if (x.className === "nav-bar") {
x.className += " responsive";
} else {
x.className = "nav-bar";
}
}
</script>
</body>
</html>

使用flexbox 简化布局

function myFunction() {
var x = document.getElementById("nav-menu");
if (x.className === "nav-bar") {
x.className += " responsive";
} else {
x.className = "nav-bar";
}
}
body {
background-color: grey;
margin: 0;
}
.nav-container {
display: flex;
background-color: white;
min-height: 80px;
width: 100%;
align-items: center;
flex-wrap: wrap;
}
#logo {
font-size: 27px;
padding: 0 13px;
height: 80px;
line-height: 80px;
}
.nav-bar {
flex-direction: row;
}
.nav-bar li {
list-style: none;
}
.nav-bar a {
color: #000;
font-size: 20px;
padding: 10px 12px;
text-decoration: none;
}
.nav-links {
margin-left: auto;
padding-right: 20px;
display: flex;
flex: 1;
justify-content: flex-end;
}
.hamburger .icon {
/* remove the styling, this code is for illustration purpose only*/
height: 40px;
width: 40px;
background: grey;
border: 1px solid #000;
}
.nav-bar,
.hamburger .icon {
display: none;
}
.hamburger {
position: absolute;
right: 20px;
top: 20px;
}
@media screen and (min-width: 901px) {
.nav-bar,
.nav-bar.responsive {
display: flex;
align-items: center;
}
}
@media screen and (max-width: 900px) {
.hamburger .icon {
display: block
}
.nav-bar.responsive {
display: flex;
flex-direction: column;
width: 100%;
margin: 0;
padding: 0;
}
.nav-links {
flex-basis: 100%;
margin: 0;
padding: 0;
}
.nav-bar li {
padding: 10px 0;
}
}
<div class="nav-container">
<div id="logo">Dellion</div>
<div class="nav-links">
<ul class="nav-bar" id="nav-menu">
<li><a class="nav-active" href="index.html">Home</a></li>
<li><a href="cars.html">Cars</a></li>
<li><a href="charities.html">Charities</a></li>
<li><a href="pros.html">Pros</a></li>
<li><a href="pros.html">Games</a></li>
<li><a href="auctions.html">Auctions</a></li>
<li><a href="support.html">Support</a></li>
</ul>
<div class="hamburger">
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i></a>
</div>
</div>
</div>

有一个非常简单的方法:

.nav-container{
background-color: white;
/* float: right; */
height: 80px;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
margin-top: 0;
}
#nav-menu{
/* float:right; */
/* padding: 13px 13px;   */
}
#nav-menu li{
display:inline-block;
font-size: 20px;
padding: 10px 12px;
text-align: center;
}
#nav-menu li a:not(.nav-active){    
color: black;   
text-decoration: none;
}
#nav-menu li a:hover:not(#logo){
color: #0aaaa0; 
}
.nav-active {
color: #0aaaa0;
text-decoration: none;
}
/* LOGO */
#logo{
padding: 0px 13px;
/* float: left; */
font-size: 27px;
}
/* Hide the link that should open and close the topnav on small screens */
#nav-menu .icon {
display: none;
}

我建议您阅读这篇关于css Flexbox的有用文章。

我认为您的部分问题是:

Javascript:

if (x.className === "nav-bar") {
x.className += " responsive"; //add space after quotation mark, otherwise class is added adjacent
} else {
x.className = "nav-bar";
}

Css:

.nav-bar.responsive li a{
float: none;
display: block !important; /* I think this needs to be crushed with important */
text-align: left;
/* position: relative; you don't need it */
}

您可以简单地使用css

flex-direction: column;

最新更新