【发布时间】:2021-02-13 03:12:35
【问题描述】:
我正在关注this 教程,了解如何使用 html、css 和一些 javascript 制作我自己的可折叠文件。到目前为止,我已经用这样的代码得到了我想要的输出:
var question = document.getElementsByClassName("question");
var i;
for (i = 0; i < question.length; i++) {
question[i].addEventListener("click", function() {
this.classList.toggle("active");
var answer = this.nextElementSibling.nextElementSibling;
if (answer.style.maxHeight){
answer.style.maxHeight = null;
} else {
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
}
.faq {
width: 100%
}
.question {
background-color: transparent;
color: #329d9c;
cursor: pointer;
padding: 2% 10%;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 18px;
font-weight: 700;
}
.question:after {
content: "\276F"; /*up*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(-90deg);
}
.active:after {
content: "\276F"; /*down*/
font-size: 20px;
color: black;
float: left;
margin-left: -6%;
font-weight: 700;
transform: rotate(90deg);
}
.line {
width: 90%;
height: 1%;
background-color: #dddddd;
border-radius: 30px;
position: absolute;
margin-left: 10%;
}
.answer {
margin-left: 10%;
padding: 0 6%;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: white;
box-shadow: 1px 2px #929191;
border: none;
border-radius: 0 0 10px 10px;
color: black;
}
<div class="faq">
<button class="question">Question 1</button>
<div class="line"></div>
<div class="answer">
Answer 1
</div>
<button class="question">Question 2</button>
<div class="line"></div>
<div class="answer">
Answer 2
</div>
</div>
我尝试运行相同的代码并在多个选项卡上进行折叠,所以它看起来或多或少像这样:
<div class="tabs">
<div class="tab-1">
<!-- the same code as snippet --!>
</div>
<div class="tab-2">
<!-- the same code as snippet --!>
</div>
</div>
但到目前为止它还没有工作。我尝试使用 bootstrap 3 来使用手风琴,但它弄乱了我的自定义 css 样式,所以我想知道有什么办法可以解决这个问题吗?
【问题讨论】:
标签: javascript html css web-frontend