保持手风琴在刷新后保持打开。

huangapple go评论65阅读模式
英文:

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>&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>
</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(&quot;accordion&quot;);
var i;

for (i = 0; i &lt; acc.length; i++) {
  acc[i].addEventListener(&quot;click&quot;, function() {
    this.classList.toggle(&quot;active&quot;);
    var parent = this.parentElement;
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + &quot;px&quot;;
      parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + &quot;px&quot;;
    } 
  });
}

HTML:

&lt;div class=&#39;sidenav&#39;&gt;
  &lt;button class=&#39;accordion&#39;&gt;&lt;i class=&#39;fa fa-star&#39; aria-hidden=&#39;true&#39;&gt;&lt;/i&gt;&amp;nbsp; FAV&lt;/button&gt;
  &lt;button class=&#39;accordion&#39;&gt;&lt;i class=&#39;fa fa-th-large&#39; aria-hidden=&#39;true&#39;&gt;&lt;/i&gt;&amp;nbsp; SALES&lt;/button&gt;
  &lt;div class=&#39;panel&#39;&gt;
      &lt;a href=&#39;item1.php&#39;&gt;&amp;emsp;ITEM 1&lt;/a&gt;
       &lt;a href=&#39;item2.php&#39;&gt;&amp;emsp;ITEM 2&lt;/a&gt;
&lt;/div&gt;

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 &lt; acc.length; i++) {
  acc[i].addEventListener(&quot;click&quot;, function() {
    this.classList.toggle(&quot;active&quot;);
    var parent = this.parentElement;
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
      sessionStorage.setItem(parent.id, &quot;&quot;); // store it as closed
    } else {
      panel.style.maxHeight = panel.scrollHeight + &quot;px&quot;;
      parent.style.maxHeight = parseInt(parent.style.maxHeight) + panel.scrollHeight + &quot;px&quot;;
      sessionStorage.setItem(parent.id, &quot;open&quot;); // store it as open
    }
  });

  // here we are getting state from sessionStorage
  var parent = acc[i].parentElement;
  if (sessionStorage.getItem(parent.id) === &quot;open&quot;) {
    parent.querySelector(&quot;.panel&quot;).style.maxHeight = parent.querySelector(&quot;.panel&quot;).scrollHeight + &quot;px&quot;;
    parent.style.maxHeight = parseInt(parent.style.maxHeight) + parent.querySelector(&quot;.panel&quot;).scrollHeight + &quot;px&quot;;
    acc[i].classList.add(&quot;active&quot;);
  }
}

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 &gt; .content {
  display: none;
}

#accordion:target &gt; .content {
  display: block;
}

<!-- language: lang-html -->

&lt;a href=&quot;#accordion&quot;&gt;open&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;close&lt;/a&gt;

&lt;a id=&quot;accordion&quot;&gt;
  &lt;div class=&quot;content&quot;&gt;
    Content
  &lt;/div&gt;
&lt;/a&gt;

<!-- 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(&quot;item1&quot;, &quot;active&quot;);

You can see more info here.

huangapple
  • 本文由 发表于 2023年3月31日 17:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定