嗨,大家好,我想让固定在右上角的导航来自屏幕的右侧(从右到左的动画)。现在使用w3school的示例,我可以提出以下代码:
HTML:

<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times; </a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span>


SCSS:

#mySidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 60px;
transition: 0.5s;
a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}
a:hover, .offcanvas a:focus{
color: #f1f1f1;
}
.closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 55px;
    margin-left: 50px;
}


JS:

function openNav() {
document.getElementById('mySidenav').style.width = '300px';
}

function closeNav() {
document.getElementById('mySidenav').style.width = '0';
}


现在代码可以正常工作了,不是我的问题。我想使我的sidenav固定在右上角,动画从右到左,每次尝试都失败了。所以我的问题是有可能做到这一点,你们会怎么做?
这是我制作的代码笔的链接,因此您可以查看正在使用的代码https://codepen.io/Karadjordje/pen/MpKvXp

最佳答案

left: 0;更改为right: 0;

工作示例:



function openNav() {
  document.getElementById('mySidenav').style.width = '300px';
}

function closeNav() {
  document.getElementById('mySidenav').style.width = '0';
}

.hamburger {
  font-size: 30px;
  cursor: pointer;
  top: 20px;
  right: 20px;
  position: fixed;
}

#mySidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  right: 0;
  background-color: #111;
  overflow-x: hidden;
  padding-top: 60px;
  transition: 0.5s;
}

#mySidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s
}

#mySidenav a:hover,
#mySidenav .offcanvas a:focus {
  color: #f1f1f1;
}

#mySidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 55px;
  margin-left: 50px;
}

body {
  height: 200em;
  position: relative;
}

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
<span class="hamburger" onclick="openNav()">&#9776;</span>

09-30 10:07