我是javascript的新手。我用我的noobie知识写了一个简单的show / hide off canvas脚本。它有效,但不是我想要的方式。我想在用户登录时显示左#mySidenav
。它正在运行,但它隐藏了实际的信息中心内容,因为#main
的行为与#mySidenav
一样已关闭。我怎样才能做到这一点?
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}

body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 250px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}

<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Employees</a>
<a href="#">Schedules</a>
<a href="#">Clients</a>
<a href="#">Timesheets</a>
</div>
<div id="main">
<h2>Admin Dashboard</h2>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
&#13;
答案 0 :(得分:0)
如果您想默认打开sidenav,则必须为margin-left
添加#main
。试试这个:
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
background-color:rgba(0,0,0,0.4)
}
#main {
transition: margin-left .5s;
padding: 16px;
margin-left:250px;
}
如果您想默认关闭:
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
使用JsFiddle链接:https://jsfiddle.net/9u9hb839/11/
答案 1 :(得分:0)
<div id="mySidenav" class="sidenav" style="width:0;">
或
.sidenav {
width: 0px;
}
将宽度设置为0,它将停止在pageload
上默认打开sidenav答案 2 :(得分:0)
嗯,您需要在width:250px
width:0px
更改为.sidenav
.sidenav {
height: 100%;
width: 0px;
}
它会阻止sidenav的默认打开。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 0px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Employees</a>
<a href="#">Schedules</a>
<a href="#">Clients</a>
<a href="#">Timesheets</a>
</div>
<div id="main">
<h2>Admin Dashboard</h2>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
答案 3 :(得分:0)
也许你想要这个?
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}
&#13;
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 250px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
margin-left: 250px;
}
&#13;
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Employees</a>
<a href="#">Schedules</a>
<a href="#">Clients</a>
<a href="#">Timesheets</a>
</div>
<div id="main">
<h2>Admin Dashboard</h2>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
&#13;
答案 4 :(得分:0)
我建议尽可能使用CSS类,而不是直接在JS中使用值。这样可以避免在任何地方重复使用样式,并使代码更易于维护。我将如何做到这一点:
在正文中添加.nav-open
课程
<body class="nav-open">
在CSS中添加这些样式
.sidenav{
width: 0;
}
body.nav-open {
background-color:rgba(0,0,0,0.4);
}
body.nav-open #main {
margin-left: 250px;
}
body.nav-open .sidenav {
width: 250px;
}
将您的JS更改为:
function openNav() {
// Add the class
document.body.className += " nav-open ";
}
function closeNav() {
// Remove the class
document.body.className = document.body.className.replace(/\bnav-open\b/g,' ');
}
document.body.className = 'nav-open'; // Just for the demo
function openNav() {
// Add the class
document.body.className += " nav-open ";
}
function closeNav() {
// Remove the class
document.body.className = document.body.className.replace(/\bnav-open\b/g,' ');
}
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
body.nav-open {
background-color:rgba(0,0,0,0.4)
}
body.nav-open #main {
margin-left: 250px;
}
body.nav-open .sidenav {
width: 250px;
}
<div id="mySidenav" class="sidenav">
<a href="#" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Employees</a>
<a href="#">Schedules</a>
<a href="#">Clients</a>
<a href="#">Timesheets</a>
</div>
<div id="main">
<h2>Admin Dashboard</h2>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
答案 5 :(得分:0)
最简单的方法可能就是将openNav()
更改添加到默认的HTML / CSS中,以便在页面加载时看到openNav()
状态。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
background-color: rgba(0,0,0,0.4);
}
.sidenav {
height: 100%;
width: 250px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
margin-left: 250px;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Employees</a>
<a href="#">Schedules</a>
<a href="#">Clients</a>
<a href="#">Timesheets</a>
</div>
<div id="main">
<h2>Admin Dashboard</h2>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>