javascript - 手风琴面板额外扩展
问题描述
我们的网站使用带有可折叠面板的手风琴。在其中一个手风琴面板中,有几个复选框可以选中或取消选中。复选框 1 - 如果选中 - 显示另一组两个复选框,但显示隐藏复选框时手风琴面板不会展开。
当新的复选框出现时,如何使手风琴面板也展开?
正在使用以下 HTML + CSS + JS。
// Accordion panels
var acc = document.getElementsByClassName("troubleshooter-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";
}
});
}
// Hide checkboxes
function showextra() {
var checkBox = document.getElementById("check1a");
var extra = document.getElementById("checkbox1a-extra");
if (checkBox.checked == true){
extra.style.display = "block";
} else {
extra.style.display = "none";
}
}
.troubleshooter-accordion {
background-color: rgba(0,175,75,1.00);
border: 1px solid rgba(0,175,75,1.00);
color: rgba(255,255,255,1.00);
padding: 16px;
cursor: pointer;
width: 100%;
text-align: center;
outline: none;
font-size: 1.15em;
transition: 0.4s;
margin-top: 2px;
border-radius: 8px;
}
.active, .troubleshooter-accordion:hover {
background-color: rgba(255,255,255,1.00);
border: 1px solid rgba(0,145,255,1.00);
color: rgba(0,145,255,1.00);
}
.troubleshooter-panel {
padding: 0 16px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
border-radius: 8px;
}
.troubleshooter-panel h4 {
margin: 16px 0
}
.infobox {
border: 2px solid rgba(0,175,75,1.00);
border-radius: 8px;
margin-top: 16px;
padding: 24px 16px;
}
.checkbox-extra {
display: none;
margin-left: 24px;
}
<button class="troubleshooter-accordion">Accordion</button>
<div class="troubleshooter-panel">
<h4 align="center">Check the boxes below</h4>
<hr>
<p>
<label><input type="checkbox" id="check1a" name="check1a" value="check1a" class="check1a" onclick="showextra()"> Checkbox 1</label>
<div class="checkbox-extra" id="checkbox1a-extra">
<p>
<label><input type="checkbox" id="check1a1" name="check1a1" value="check1a1" class="check1a1"> Checkbox 1-A</label><br>
<label><input type="checkbox" id="check1a2" name="check1a2" value="check1a2" class="check1a2"> Checkbox 1-B</label>
</p>
</div>
<hr>
</p>
<p>
<label><input type="checkbox" id="check1b" name="check1b" value="check1b" class="check1b"> Checkbox 2</label>
</p>
<hr>
</div>
解决方案
当您第一次展开手风琴时,您通过以下方式设置它的最大高度值:
panel.style.maxHeight = panel.scrollHeight + "px";
当您第二次展开手风琴时,这当然应该再次发生,否则最大高度保持不变。
要解决此问题,您只需从复选框中查找父面板并再次设置最大高度:
var panel = checkBox.closest('.troubleshooter-panel');
panel.style.maxHeight = panel.scrollHeight + "px";
完整代码:
// Accordion panels
var acc = document.getElementsByClassName("troubleshooter-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";
}
});
}
// Hide checkboxes
function showextra() {
var checkBox = document.getElementById("check1a");
var extra = document.getElementById("checkbox1a-extra");
if (checkBox.checked == true){
extra.style.display = "block";
var panel = checkBox.closest('.troubleshooter-panel');
panel.style.maxHeight = panel.scrollHeight + "px";
} else {
extra.style.display = "none";
var panel = checkBox.closest('.troubleshooter-panel');
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
.troubleshooter-accordion {
background-color: rgba(0,175,75,1.00);
border: 1px solid rgba(0,175,75,1.00);
color: rgba(255,255,255,1.00);
padding: 16px;
cursor: pointer;
width: 100%;
text-align: center;
outline: none;
font-size: 1.15em;
transition: 0.4s;
margin-top: 2px;
border-radius: 8px;
}
.active, .troubleshooter-accordion:hover {
background-color: rgba(255,255,255,1.00);
border: 1px solid rgba(0,145,255,1.00);
color: rgba(0,145,255,1.00);
}
.troubleshooter-panel {
padding: 0 16px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
border-radius: 8px;
}
.troubleshooter-panel h4 {
margin: 16px 0
}
.infobox {
border: 2px solid rgba(0,175,75,1.00);
border-radius: 8px;
margin-top: 16px;
padding: 24px 16px;
}
.checkbox-extra {
display: none;
margin-left: 24px;
}
<button class="troubleshooter-accordion">Accordion</button>
<div class="troubleshooter-panel">
<h4 align="center">Check the boxes below</h4>
<hr>
<p>
<label><input type="checkbox" id="check1a" name="check1a" value="check1a" class="check1a" onclick="showextra()"> Checkbox 1</label>
<div class="checkbox-extra" id="checkbox1a-extra">
<p>
<label><input type="checkbox" id="check1a1" name="check1a1" value="check1a1" class="check1a1"> Checkbox 1-A</label><br>
<label><input type="checkbox" id="check1a2" name="check1a2" value="check1a2" class="check1a2"> Checkbox 1-B</label>
</p>
</div>
<hr>
</p>
<p>
<label><input type="checkbox" id="check1b" name="check1b" value="check1b" class="check1b"> Checkbox 2</label>
</p>
<hr>
</div>
推荐阅读
- java - 添加 JLabel 和 JButton 后看不到 JTextField
- java - 在 Spring Boot / FreeMarker 中加载图像时出现问题
- javascript - Javascript不调用函数
- optimization - 优化 applescript 以减少能源影响
- android - 无法在 Android Studio 3.1.4 中放置智能横幅广告 & 应用程序崩溃。我应该使用哪些依赖项?
- java - 此方法调用为非空方法参数传递空值。要么将参数注释为应始终为非空的参数
- javascript - 与 vuex store + cookie 有关的问题
- r - Ggplot2:更改单个值的颜色
- neo4j - 使用 WHERE 排除节点不起作用
- scala - dataframe spark scala take the (MAX-MIN) for each group