简体   繁体   English

手风琴打开按钮

[英]Accordion Open Button

An accordion that I'm editing on Codepen is running well.我在 Codepen 上编辑的手风琴运行良好。 However, when clicking on more than one question, I want the previous answers to close when when the current answer opens.但是,当单击多个问题时,我希望在当前答案打开时关闭先前的答案。 Any clues how I can do that?任何线索我怎么能做到这一点?

Codepen here在这里写代码

Also , when using Font Awesome 4.7.0 the following unicodes show up properly.此外,当使用 Font Awesome 4.7.0 时,以下 unicode 会正确显示。

(f055 & f056) Unicodes

However with switching to Font Awesome 5.14.5 the Unicodes are no longer working.然而,随着切换到 Font Awesome 5.14.5,Unicode 不再工作。

to close other, loop again all button inside onclick , for font, replace font-family: "FontAwesome";要关闭其他,请再次循环onclick内的所有按钮,对于字体,替换font-family: "FontAwesome"; with font-family: "Font Awesome\ 5 Free"; with font-family: "Font Awesome\ 5 Free";

 const accordionBtns = document.querySelectorAll(".accordion"); accordionBtns.forEach((accordion) => { accordion.onclick = function() { this.classList.toggle("is-open"); let content = this.nextElementSibling; if (content.style.maxHeight) { content.style.maxHeight = null; } else { // add this line accordionBtns.forEach(acc => acc.nextElementSibling.style.maxHeight = null) content.style.maxHeight = content.scrollHeight + "px"; } }; });
 .faq-container { width: 80%; max-width: 600px; margin: 50px auto; } button.accordion { width: 100%; background: #C0C0C0; border: none; outline: none; text-align: left; padding: 15px 20px; font-size: 18px; font-weight: bold; color: #4169E1; cursor: pointer; transition: background-color 2.2s linear; } button.accordion:after { content: "\f055"; font-family: "Font Awesome\ 5 Free"; font-size: 18px; float: right; } button.accordion.is-open:after { content: "\f056"; } button.accordion:hover, button.accordion.is-open { color: #FFF; background: #4169E1; /*border-left: 2px solid #C0C0C0; border-right: 2px solid #C0C0C0; border-top: 2px solid #C0C0C0; border-bottom: 2px solid #C0C0C0;*/ }.accordion-content { font-family: Arial; font-weight: bold; color: #663399; background: #FFF; border-left: 2px solid #C0C0C0; border-right: 2px solid #C0C0C0; border-bottom: 2px solid #C0C0C0; padding: 0 20px; max-height: 0; overflow: hidden; transition: max-height 0.2s ease-in-out; }
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css"> <div class="faq-container"> <h1 style="text-align: center; font-family: Arial; color: #663399">FAQ Accordion</h1> <button class="accordion">When Is It?</button> <div class="accordion-content"> <p> On The Date That We Select. <br> On The Date That We Select. </p> </div> <button class="accordion">Why Is It.</button> <div class="accordion-content"> <p> Because We Chose To. </p> </div> <button class="accordion">Where Is It</button> <div class="accordion-content"> <p> At The Place That We Select. </p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM