我正在创建可以打开和关闭的侧面导航。问题是导航项目的标题在关闭时也被折叠了,我想解决这个问题。
赞
css
.sideNav {
height: 100%;
width: 0;
position: fixed;
z-index: 999;
top: 0;
left: 0;
background-color: #263238;
overflow-x: hidden;
transition: all 0.3s;
padding-top: 60px;
}
.sideNav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #fff;
display: block;
transition: 0.1s;
}
html
<ul id="sideNav" class="sideNav nav flex-column" style="width: 250px;"><li class="nav-item"><a href="#" class="nav-link>Title here</a></li></ul>
打开和关闭时,我会更改导航的宽度。
js
document.getElementById("sideNav").style.width = "250px"
document.getElementById('sideNav').style.width = "0"
答案 0 :(得分:1)
诀窍是使用等于导航栏宽度的负margin
。当关闭时,它具有负的保证金,而当打开时,它具有零的保证金。例如:
.sideNav {
margin-left: -250px;
}
开张时
document.getElementById("sideNav").style.marginLeft = "0";
收盘时
document.getElementById("sideNav").style.marginLeft = "-250px";