关闭导航栏时,侧面导航上的项目名称也会折叠

时间:2019-04-14 13:00:38

标签: html css bootstrap-4 nav

我正在创建可以打开和关闭的侧面导航。问题是导航项目的标题在关闭时也被折叠了,我想解决这个问题。

enter image description here

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"

1 个答案:

答案 0 :(得分:1)

诀窍是使用等于导航栏宽度的负margin。当关闭时,它具有负的保证金,而当打开时,它具有零的保证金。例如:

.sideNav {
   margin-left: -250px;
}

开张时

document.getElementById("sideNav").style.marginLeft = "0";

收盘时

document.getElementById("sideNav").style.marginLeft = "-250px";