JavaScript Navbar不应响应,因为它应该是
我尝试使用CSS和JS自定义W3学校响应式Navbar。我想添加一个网页的徽标和标题,然后添加Navbar。当网页最大化时,它可以正常工作,但是当我使其较小时,navbar零件溢出,然后单击菜单时,菜单图标消失了,除非刷新,否则我无法返回原始表格 代码笔链接
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav 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: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
header {
background-color: rgba(2, 13, 24, 1);
position: fixed;
top: 0;
left: 0;
right: 0;
height: 60px;
display: flex;
align-items: center;
box-shadow: 0 0 25px 0 rgba(2, 13, 24, 1);
}
header * {
display: inline;
}
img.logo {
height: 45px;
margin-left: 40px;
}
h6.title {
padding: 20px;
color: white;
font-family: "Rubik", sans-serif;
font-weight: 500;
font-size: larger;
}
.topnav {
background-color: rgba(2, 13, 24, 1);
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-family: "Rubik", sans-serif;
font-weight: 400;
font-size: medium;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: rgba(35, 58, 80, 1);
color: white;
}
/* Add an active class to highlight the current page */
.topnav a.active {
color: rgba(219, 255, 2, 1);
}
/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
display: none;
}
body {
margin: 0px;
background: linear-gradient(
to bottom,
rgba(30, 59, 112, 0.6),
rgba(2, 13, 24, 0.6)
)
no-repeat center center fixed,
url("../Images/Background.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
main {
color: white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="..\Css\style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;600&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<img src="https://www.freeiconspng.com/uploads/cinema-movie-theatre-icon-2.png" class="logo">
<h6 class="title">MoviesHub</h6>
<div class="topnav" id="myTopnav">
<ul>
<li><a href="#home" class="active">Home</a></li>
<li><a href="#news">Movies</a></li>
<li><a href="#contact">Celebrities</a></li>
<li> <a href="#about">About</a></li>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</ul>
</div>
</header>
<main>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consequat libero eget magna commodo, quis pharetra tellus pretium. Sed viverra ante in mauris finibus dapibus. Maecenas congue dapibus nulla, eu gravida orci consequat eu. Phasellus nec nunc malesuada, aliquam massa ac, accumsan metus. Fusce sed dignissim lectus. Nunc elit tellus, sollicitudin ac accumsan ut, egestas et dui. Maecenas aliquam est a ligula scelerisque, in aliquam neque sodales. Nullam condimentum euismod dictum. Curabitur non ex elementum, pretium enim ut, ornare ipsum.</p>
</main>
</body>
</html>
I tried customizing w3 school responsive navbar using CSS and JS. I wanted to add a logo and a title of the web page and then the navbar. When the webpage is maximized it works fine but when I make it smaller then the navbar part overflows and when clicked to drop down the menu the menu icon vanishes and I cant return to the original form unless refreshed
code pen link
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav 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: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
header {
background-color: rgba(2, 13, 24, 1);
position: fixed;
top: 0;
left: 0;
right: 0;
height: 60px;
display: flex;
align-items: center;
box-shadow: 0 0 25px 0 rgba(2, 13, 24, 1);
}
header * {
display: inline;
}
img.logo {
height: 45px;
margin-left: 40px;
}
h6.title {
padding: 20px;
color: white;
font-family: "Rubik", sans-serif;
font-weight: 500;
font-size: larger;
}
.topnav {
background-color: rgba(2, 13, 24, 1);
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-family: "Rubik", sans-serif;
font-weight: 400;
font-size: medium;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: rgba(35, 58, 80, 1);
color: white;
}
/* Add an active class to highlight the current page */
.topnav a.active {
color: rgba(219, 255, 2, 1);
}
/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
display: none;
}
body {
margin: 0px;
background: linear-gradient(
to bottom,
rgba(30, 59, 112, 0.6),
rgba(2, 13, 24, 0.6)
)
no-repeat center center fixed,
url("../Images/Background.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
main {
color: white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="..\Css\style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;600&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<img src="https://www.freeiconspng.com/uploads/cinema-movie-theatre-icon-2.png" class="logo">
<h6 class="title">MoviesHub</h6>
<div class="topnav" id="myTopnav">
<ul>
<li><a href="#home" class="active">Home</a></li>
<li><a href="#news">Movies</a></li>
<li><a href="#contact">Celebrities</a></li>
<li> <a href="#about">About</a></li>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</ul>
</div>
</header>
<main>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consequat libero eget magna commodo, quis pharetra tellus pretium. Sed viverra ante in mauris finibus dapibus. Maecenas congue dapibus nulla, eu gravida orci consequat eu. Phasellus nec nunc malesuada, aliquam massa ac, accumsan metus. Fusce sed dignissim lectus. Nunc elit tellus, sollicitudin ac accumsan ut, egestas et dui. Maecenas aliquam est a ligula scelerisque, in aliquam neque sodales. Nullam condimentum euismod dictum. Curabitur non ex elementum, pretium enim ut, ornare ipsum.</p>
</main>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己完成了,我使用MediaQuery获取NAV栏的最大宽度(徽标和菜单)奖励在脚本标签中添加了jQuery,以更改菜单图标,以在打开
CSS时关闭图标
Got it done by myself, I used Mediaquery to get the max width of the nav bar ( both logo and menu) bonus added jquery within script tag to change the menu icon to close icon when opened
Css