gpt4 book ai didi

javascript - Sidenav 覆盖固定在右上角

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:24 25 4
gpt4 key购买 nike

大家好,我正在尝试让固定在右上角的导航来自屏幕的右侧(从右到左的动画)。现在使用来自 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 固定在右上角,动画从右到左,我所做的每一次尝试都完全失败了。所以我的问题是有可能做到这一点,你们会怎么做????这是我制作的 codepen 的链接,因此您可以查看实际代码 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>

关于javascript - Sidenav 覆盖固定在右上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com