如何从网页的左到右对齐该导航栏?

时间:2018-10-05 13:00:04

标签: javascript html css

我正在该网站上工作,并将此导航栏添加到浏览器的左侧。我需要有关更改导航栏侧面的帮助,因此与其从左侧打开,不应该从浏览器的右侧打开。

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()">&times;</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <span class="rightalign" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span>
</div>

1 个答案:

答案 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()">&times;</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <span class="rightalign" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span>
</div>