英文:
Keep accordion open after refresh
问题
我有一个带有手风琴菜单的边栏,但当我刷新页面或转到另一个页面时,它会关闭。如何让它在这些操作后保持打开状态?以下是用CSS的示例:https://jsfiddle.net/ag9q1tvc/
JavaScript:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var parent = this.parentElement;
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + "px";
}
});
}
HTML:
<div class='sidenav'>
<button class='accordion'><i class='fa fa-star' aria-hidden='true'></i> FAV</button>
<button class='accordion'><i class='fa fa-th-large' aria-hidden='true'></i> SALES</button>
<div class='panel'>
<a href='item1.php'> ITEM 1</a>
<a href='item2.php'> ITEM 2</a>
</div>
</div>
我在其他答案中找不到解决我的问题的确切方法。
谢谢!
英文:
I have an sidenav with accordion menus, but when I refresh or go to another page it closes. How can it be stay open after these actions? Example here with CSS: https://jsfiddle.net/ag9q1tvc/
JavaScript:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var parent = this.parentElement;
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + "px";
}
});
}
HTML:
<div class='sidenav'>
<button class='accordion'><i class='fa fa-star' aria-hidden='true'></i>&nbsp; FAV</button>
<button class='accordion'><i class='fa fa-th-large' aria-hidden='true'></i>&nbsp; SALES</button>
<div class='panel'>
<a href='item1.php'>&emsp;ITEM 1</a>
<a href='item2.php'>&emsp;ITEM 2</a>
</div>
I couldn't find the exact method for my problem in other answers.
Thank you!
答案1
得分: 1
你需要在某个地方存储手风琴的状态,可以使用 sessionStorage 或 localStorage,也称为 cookies。对我来说,sessionStorage 是最好的方法,下面是一个你可以做的示例:
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var parent = this.parentElement;
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
sessionStorage.setItem(parent.id, ""); // 存储为关闭状态
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + "px";
sessionStorage.setItem(parent.id, "open"); // 存储为打开状态
}
});
// 这里我们从sessionStorage中获取状态
var parent = acc[i].parentElement;
if (sessionStorage.getItem(parent.id) === "open") {
parent.querySelector(".panel").style.maxHeight = parent.querySelector(".panel").scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + parent.querySelector(".panel").scrollHeight + "px";
acc[i].classList.add("active");
}
}
请注意,它将状态存储在浏览器关闭之前,否则你可以使用 cookies。
英文:
You need to store accordion state somewhere, it can be sessionStorage or localStorage also called cookies. As for me sessionStorage is the best way of doing that, here is an example of what you can do:
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var parent = this.parentElement;
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
sessionStorage.setItem(parent.id, ""); // store it as closed
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + "px";
sessionStorage.setItem(parent.id, "open"); // store it as open
}
});
// here we are getting state from sessionStorage
var parent = acc[i].parentElement;
if (sessionStorage.getItem(parent.id) === "open") {
parent.querySelector(".panel").style.maxHeight = parent.querySelector(".panel").scrollHeight + "px";
parent.style.maxHeight = parseInt(parent.style.maxHeight) + parent.querySelector(".panel").scrollHeight + "px";
acc[i].classList.add("active");
}
}
Note that it will store state until browser is closed, otherwise you can use cookies.
答案2
得分: 1
:target选择器和锚标记(无JS)
:target
选择器在元素的id
等于URL中#
后的哈希值时匹配。由于浏览器在重新加载或导航时保留哈希,这将在URL中保持手风琴的状态。
- 优点:不需要JavaScript,跨浏览器兼容
- 缺点:如果您需要
#
用于其他用途(如单页路由),这不是一个选项。
#accordion {
display: block;
border: 2px solid blue;
}
#accordion > .content {
display: none;
}
#accordion:target > .content {
display: block;
}
<a href="#accordion">打开</a>
<a href="#">关闭</a>
<a id="accordion">
<div class="content">
内容
</div>
</a>
英文:
:target Selector and anchor tag (no JS)
The :target selector matches if the id of an element is equal to the hash (#) after the url. Since the browser keeps the hash with reload or navigation, this will persist the state of the accordion in the url.
- PRO: No JavaScript needed, cross-browser compatible
- CON: If you need the # for something else (like single page routing) this is not an option.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#accordion {
display: blocK;
border: 2px solid blue;
}
#accordion > .content {
display: none;
}
#accordion:target > .content {
display: block;
}
<!-- language: lang-html -->
<a href="#accordion">open</a>
<a href="#">close</a>
<a id="accordion">
<div class="content">
Content
</div>
</a>
<!-- end snippet -->
答案3
得分: 0
你可以尝试使用本地存储,它是存储在浏览器缓存中的变量。
localStorage.setItem("item1", "active");
您可以在这里查看更多信息。
英文:
You can try to use local storage, it is a variable that is store in the cache of the browser
localStorage.setItem("item1", "active");
You can see more info here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论