我有一个手风琴菜单,这是通用的所有页面。 当用户点击子菜单时,它会将用户带到该链接,但是由于加载了一个全新的页面,菜单已经折叠回来了。
有没有使用普通JavaScript保存菜单状态的方法? 我见过使用Jquery的解决方案,但它并不在使用中。
下面我有我的菜单片段供参考。
null
.nav_bar {
font-family: sans-serif;
font-size: 20px;
width: 250px;
height: 100vh;
position: fixed;
top: 0px;
left: 0px;
}
.nav_bar .nav_header {
text-align: center;
margin: 0;
padding: 0;
}
.nav_bar .nav_header .nav_logo {
width: 180px;
}
.nav_bar .menu {
margin: 20px;
overflow: hidden;
}
.nav_bar .menu .item {
display: block;
margin: 20px;
position: relative;
}
.nav_bar .menu .item .sub_menu {
overflow: hidden;
-webkit-transition: 0.3s;
transition: 0.3s;
max-height: 0;
}
.nav_bar .menu .item .sub_menu a::before {
content: '';
width: 2px;
background: #280F98;
position: absolute;
top: 0;
left: 0;
height: 100%;
}
.nav_bar .menu .item .sub_menu a {
display: block;
font-size: 14px;
padding: 10px 5px 10px 10px;
position: relative;
}
.nav_bar .menu .item:target .sub_menu {
max-height: 10em;
}
<nav class="nav_bar">
<div class="menu">
<div class="item" id="our_work">
<a href="#our_work" class="button">Our Work</a>
<div class="sub_menu">
<a href="our_work">Our mission</a>
<a href="our_work#goals">Goals</a>
<a href="our_work#initiatives">Initiatives</a>
</div>
</div>
<div class="item" id="performances">
<a href="#performances" class="button">Showcase</a>
<div class="sub_menu">
<a href="#">2020</a>
<a href="#">2019</a>
<a href="#">2018</a>
<a href="#">2017</a>
</div>
</div>
<div class="item" id="concerts">
<a href="#concerts" class="button">Talent</a>
<div class="sub_menu">
<a href="#">Concerts 1</a>
<a href="#">Concerts 2</a>
<a href="#">Concerts 3</a>
</div>
</div>
<div class="item" id="shop">
<a href="#shop" class="button">Shop</a>
<div class="sub_menu">
<a href="#">Shop 1</a>
<a href="#">Shop 2</a>
<a href="#">Shop 3</a>
</div>
</div>
</div>
</nav>
null
试试这个。 我用href:js
添加了so test
菜单项,因为这是控制台日志中的代码段位置链接。
菜单打开基于a href
链接和当前位置。
编辑:
按照注释中的要求,在所有主菜单类别上添加了点击事件。 如果单击了一个,它将首先隐藏所有子菜单(如果之前显示了任何子菜单,则隐藏),然后只显示单击的子菜单项。
null
window.addEventListener("load", function() {
const page = location.href.split("/").pop();
console.log(location.href);
console.log(page);
[...document.querySelectorAll(".nav_bar > .menu > .item > .sub_menu a")].forEach(link => {
if (link.href.endsWith(page)) {
link.setAttribute("style", "background-color: red;");
link.parentElement.setAttribute("style", "max-height: 10em");
}
})
})
const menus = document.querySelectorAll(".nav_bar > .menu > .item ")
for (const menu of menus) {
menu.addEventListener('click', function(event) {
[...document.querySelectorAll(".nav_bar > .menu > .item > .sub_menu a")].forEach(link => {
link.parentElement.setAttribute("style", "max-height: 0em");
})
this.getElementsByClassName("sub_menu")[0].setAttribute("style", "max-height: 10em");
})
}
.nav_bar {
font-family: sans-serif;
font-size: 20px;
width: 250px;
height: 100vh;
position: fixed;
top: 0px;
left: 0px;
}
.nav_bar .nav_header {
text-align: center;
margin: 0;
padding: 0;
}
.nav_bar .nav_header .nav_logo {
width: 180px;
}
.nav_bar .menu {
margin: 20px;
overflow: hidden;
}
.nav_bar .menu .item {
display: block;
margin: 20px;
position: relative;
}
.nav_bar .menu .item .sub_menu {
overflow: hidden;
-webkit-transition: 0.3s;
transition: 0.3s;
max-height: 0;
}
.nav_bar .menu .item .sub_menu a::before {
content: '';
width: 2px;
background: #280F98;
position: absolute;
top: 0;
left: 0;
height: 100%;
}
.nav_bar .menu .item .sub_menu a {
display: block;
font-size: 14px;
padding: 10px 5px 10px 10px;
position: relative;
}
.nav_bar .menu .item:target .sub_menu {
max-height: 10em;
}
<nav class="nav_bar">
<div class="menu">
<div class="item" id="our_work">
<a href="#our_work" class="button">Our Work</a>
<div class="sub_menu">
<a href="js">SO TEST</a>
<a href="our_work">Our mission</a>
<a href="our_work#goals">Goals</a>
<a href="our_work#initiatives">Initiatives</a>
</div>
</div>
<div class="item" id="performances">
<a href="#performances" class="button">Showcase</a>
<div class="sub_menu">
<a href="#">2020</a>
<a href="#">2019</a>
<a href="#">2018</a>
<a href="#">2017</a>
</div>
</div>
<div class="item" id="concerts">
<a href="#concerts" class="button">Talent</a>
<div class="sub_menu">
<a href="#">Concerts 1</a>
<a href="#">Concerts 2</a>
<a href="#">Concerts 3</a>
</div>
</div>
<div class="item" id="shop">
<a href="#shop" class="button">Shop</a>
<div class="sub_menu">
<a href="#">Shop 1</a>
<a href="#">Shop 2</a>
<a href="#">Shop 3</a>
</div>
</div>
</div>
</nav>