我遇到一个问题,即页面加载时打开了我的汉堡菜单。除此之外,它还可以按需运行。如果我单击它,它将关闭,然后也打开onclick。
<div class="topnav">
<a href="#home" class="active">Dashboard</a>
<!-- Navigation links (hidden by default) -->
<div id="nav">
<a href="/index.php"><i class="fas fa-user-circle"></i>Profile</a>
<a href="../user/invoices.php"><i class="fas fa-user-circle"></i>Invoices</a>
<a href="/post"><i class="fas fa-user-circle"></i>Post Message</a>
<a href="/register"><i class="fas fa-user-circle"></i>New User</a>
<a href="/logout.php"><i class="fas fa-sign-out-alt"></i>Logout</a>
</div>
<!-- "Hamburger menu" / "Bar icon" to toggle the navigation links -->
<a href="javascript:void();" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<script>
/* Toggle between showing and hiding the navigation menu links when the user clicks on the hamburger menu / bar icon */
function myFunction() {
var x = document.getElementById("nav");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
</script>
CSS
* {
font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
background-color: #435165;
}
.login {
width: 400px;
background-color: #ffffff;
box-shadow: 0 0 9px 0 rgba(0, 0, 0, 0.3);
margin: 100px auto;
}
.login h1 {
text-align: center;
background-color: #737373;
color: #ffffff;
font-size: 24px;
padding: 20px 0 20px 0;
border-bottom: 1px solid #dee0e4;
}
.login form {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding-top: 20px;
}
.login form label {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
background-color: #737373;
color: #ffffff;
}
.login form input[type="password"], .login form input[type="text"] {
width: 310px;
height: 50px;
border: 1px solid #dee0e4;
margin-bottom: 20px;
padding: 0 15px;
}
.login form input[type="submit"] {
width: 100%;
padding: 15px;
margin-top: 20px;
background-color: #737373;
border: 0;
cursor: pointer;
font-weight: bold;
color: #ffffff;
transition: background-color 0.2s;
}
.login form input[type="submit"]:hover {
background-color: #ef1d1d;
transition: background-color 0.2s;
}
.content {
width: 100%;
margin: 0 auto;
}
.content h2 {
margin: 0;
padding: 25px 0;
font-size: 22px;
border-bottom: 1px solid #e0e0e3;
color: #4a536e;
}
.content > p, .content > div {
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
margin: 25px 0;
padding: 25px;
background-color: #fff;
}
.content > p table td, .content > div table td {
padding: 5px;
}
.content > p table td:first-child, .content > div table td:first-child {
font-weight: bold;
color: #4a536e;
padding-right: 15px;
}
.content > div p {
padding: 5px;
margin: 0 0 10px 0;
}
/* For Posting announcements */
.form-contact {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-contact .form-contact-heading,
.form-contact .checkbox {
margin-bottom: 10px;
}
.form-contact .checkbox {
font-weight: normal;
}
.form-contact .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-contact .form-control:focus {
z-index: 2;
}
.form-contact input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.container { background-color: #435165;}
@media print {
body {
display: none;
}
}
.col-md-12{
background-color: white;
}
/* Style the navigation menu */
.topnav {
overflow: hidden;
background-color: #333;
position: relative;
}
/* Hide the links inside the navigation menu (except for logo/home) */
.topnav #myLinks {
display: none;
}
/* Style navigation menu links */
.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
/* Style the hamburger menu */
.topnav a.icon {
background: #ef1d1d;
display: block;
position: absolute;
right: 0;
top: 0;
}
/* Add a grey background color on mouse-over */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Style the active link (or home/logo) */
.active {
background-color: #737373;
color: white;
}
在上面的脚本中什么可能导致此问题?我到处都看了看。 javascript似乎正确,但我显然缺少某些内容。
答案 0 :(得分:4)
您需要将此添加到CSS:
#nav{
display:none;
}
默认情况下,即在页面加载时,它将隐藏您的#nav
div
。另外,您可以使用Javascript通过添加以下内容来隐藏页面加载时的#nav
:
// self executing function
(function() {
var x = document.getElementById("nav");
x.style.display = "none";
})();
这将使您的myFunction();
方法可以在页面加载时使用。
因为这样的菜单通常很合适,并且因为它只有4行,所以您可能要考虑使用第三种方法,如果/当重新加载页面时,它将保持菜单的切换状态:
// self executing function
(function() {
if(window.localStorage.getItem('nav') === null){
window.localStorage.setItem('nav', 'none');
}
var x = document.getElementById("nav");
x.style.display = window.localStorage.getItem('nav');
})();
function myFunction() {
var x = document.getElementById("nav");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
window.localStorage.setItem('nav', x.style.display);
}
答案 1 :(得分:1)
您可以像这样使用CSS:
#nav{
display:none;
}
但是,如果您想在身体加载时做一些事情,可以添加如下函数:
<body onload="onload()">
和功能:
function onload() {
var x = document.getElementById("nav");
x.style.display = "none";
}
在此处查看: Fiddle