如何在页面加载时保持手风琴面板打开,但之后保持其可折叠性?
How can I keep my accordion panels open when the page loads, but maintain its collapsibility afterwards?
对于我的手风琴菜单,我很难弄清楚如何在浏览器加载时让我的手风琴面板保持打开状态。我试过删除 overflow:hidden;对于面板,但我 运行 出错了。我希望能够在浏览器加载时打开我的面板,但之后让用户 open/close 链接。
HTML
<div class="sideMenu">
<div class="logo">TS<div>
<div class="smallSpacer"></div>
<div class="accordion">code</div>
<div class="panel">
<p>uhn redesign </p>
<p>180 websites </p>
</div>
<div class="bigSpacer"></div>
<div class="accordion">design</div>
<div class="panel">
<p>gigloo </p>
<p>space is the place</p>
<p>tyler the creator </p>
<p>nike concepts</p>
<p>portraits/fashion</p>
</div>
<div class="bigSpacer"></div>
<div class="accordion">personal</div>
<div class="panel">
<p>product</p>
<p>animations</p>
<p>comics</p>
</div>
</div>
CSS
body{
font-family: 'Roboto Mono', monospace;
}
.sideMenu{
background-color:black;
width:250px;
height:100%;
position:fixed;
margin:20px;
text-align:center;
}
.logo{
color:white;
}
.smallSpacer{
margin-bottom:15px;
}
.bigSpacer{
margin-bottom:20px;
}
.accordion {
color: white;
cursor: pointer;
transition: 0.4s;
font-weight:500;
}
.active, .accordion:hover {
}
.panel {
font-weight:300;
max-height: 0;
overflow: hidden;
transition: max-height 0.1s ease-out;
}
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 panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
这是我的解决方案:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
//this is where magic happens
if(i==0){
acc[0].classList.toggle("active");
var panel = acc[0].nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
如您所见,我所做的只是展开第一个元素的手风琴。
我已经复制并粘贴了事件侦听器将执行的相同代码,而是在启动时直接将其附加到第一个元素。这也有助于保持可折叠性,因为已添加事件侦听器,而不管其他代码如何。
希望对您有所帮助!
对于我的手风琴菜单,我很难弄清楚如何在浏览器加载时让我的手风琴面板保持打开状态。我试过删除 overflow:hidden;对于面板,但我 运行 出错了。我希望能够在浏览器加载时打开我的面板,但之后让用户 open/close 链接。
HTML
<div class="sideMenu">
<div class="logo">TS<div>
<div class="smallSpacer"></div>
<div class="accordion">code</div>
<div class="panel">
<p>uhn redesign </p>
<p>180 websites </p>
</div>
<div class="bigSpacer"></div>
<div class="accordion">design</div>
<div class="panel">
<p>gigloo </p>
<p>space is the place</p>
<p>tyler the creator </p>
<p>nike concepts</p>
<p>portraits/fashion</p>
</div>
<div class="bigSpacer"></div>
<div class="accordion">personal</div>
<div class="panel">
<p>product</p>
<p>animations</p>
<p>comics</p>
</div>
</div>
CSS
body{
font-family: 'Roboto Mono', monospace;
}
.sideMenu{
background-color:black;
width:250px;
height:100%;
position:fixed;
margin:20px;
text-align:center;
}
.logo{
color:white;
}
.smallSpacer{
margin-bottom:15px;
}
.bigSpacer{
margin-bottom:20px;
}
.accordion {
color: white;
cursor: pointer;
transition: 0.4s;
font-weight:500;
}
.active, .accordion:hover {
}
.panel {
font-weight:300;
max-height: 0;
overflow: hidden;
transition: max-height 0.1s ease-out;
}
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 panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
这是我的解决方案:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
//this is where magic happens
if(i==0){
acc[0].classList.toggle("active");
var panel = acc[0].nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
如您所见,我所做的只是展开第一个元素的手风琴。 我已经复制并粘贴了事件侦听器将执行的相同代码,而是在启动时直接将其附加到第一个元素。这也有助于保持可折叠性,因为已添加事件侦听器,而不管其他代码如何。
希望对您有所帮助!