我正在该网站上工作,并将此导航栏添加到浏览器的左侧。我需要有关更改导航栏侧面的帮助,因此与其从左侧打开,不应该从浏览器的右侧打开。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: dodgerblue;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 40px;
text-decoration: none;
font-size: 25px;
color: white;
display: block;
transition: 0.3s;
}
.rightalign {
display: flex;
justify-content: flex-end;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .3s;
padding: 16px;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
<div id="main">
<span class="rightalign" style="font-size:30px;cursor:pointer" onclick="openNav()">☰</span>
</div>
答案 0 :(得分:1)
在right: 0
上使用left: 0
代替.sidenav
,并在margin-right
上过渡margin-left
而不是#main
。
要左对齐关闭按钮,请将right: 25px
更改为left: 0
,将margin-left: 50px
更改为margin-right: 50px
。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginRight = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginRight = "0";
}
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
right: 0;
background-color: dodgerblue;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 40px;
text-decoration: none;
font-size: 25px;
color: white;
display: block;
transition: 0.3s;
}
.rightalign {
display: flex;
justify-content: flex-end;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
left: 0;
font-size: 36px;
margin-right: 50px;
}
#main {
transition: margin-right .3s;
padding: 16px;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
<div id="main">
<span class="rightalign" style="font-size:30px;cursor:pointer" onclick="openNav()">☰</span>
</div>