css W3C的 accordion -如何一次打开一个面板?

wmomyfyw  于 2023-02-20  发布在  其他
关注(0)|答案(3)|浏览(138)

我有一个 accordion ,我想一次只打开一个面板。我需要一些帮助,能够关闭任何打开的面板时,用户点击打开一个关闭的面板。我猜一些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";
    } 
  });
}
.accordion {
    background-color: #EA3F8A;
    color: #FFF;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

.active, .accordion:hover {
    background-color: #C11561;
}

.accordion:after {
    content: '\002B';
    color: #FFF;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

.active:after {
    content: "\2212";
}

.panel {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}

#accord {
    margin-bottom: 5.5em;
}
<button class="accordion">Section 1</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    
    <button class="accordion">Section 2</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <button class="accordion">Section 2</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
pgvzfuti

pgvzfuti1#

您需要先关闭其他折叠面板,然后再打开单击的折叠面板。

var acc = document.getElementsByClassName("accordion");
var i, j;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    for (j = 0; j < acc.length; j++) {
      acc[j].classList.remove("active");
      acc[j].nextElementSibling.style.maxHeight = null;
    }
    if (!this.classList.contains("active")){
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.maxHeight){
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      } 
    }

  });
}
.accordion {
    background-color: #EA3F8A;
    color: #FFF;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

.active, .accordion:hover {
    background-color: #C11561;
}

.accordion:after {
    content: '\002B';
    color: #FFF;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

.active:after {
    content: "\2212";
}

.panel {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}

#accord {
    margin-bottom: 5.5em;
}
<button class="accordion">Section 1</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    
    <button class="accordion">Section 2</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <button class="accordion">Section 2</button>
    <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
v2g6jxz6

v2g6jxz62#

我一直觉得自己做 accordion 更容易:

$('.item-title').click(function() {
        $(this).parents().siblings().find('.item-body').slideUp();
        $(this).parents().siblings('.item').find('.item-open-close').removeClass('fa-minus').addClass('fa-plus');
        $(this).find('i').toggleClass('fa-minus fa-plus');
        $(this).siblings('.item-body').slideToggle();
    });
.item-title i {
float:right;
}

.item-title {
    background-color: #EA3F8A;
    color: #FFF;
    cursor: pointer;
    padding: 18px;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}
.item-body {
padding:18px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div class="accordion">            
  <div class="item">
    <div class="item-title">
      Section 1<i class="fa item-open-close fa-plus"></i>
    </div>
    <div class="item-body" style="display: none;">
      This is content. This is content. This is content. This is content. This is content. This is content. This is content.   
    </div>  
  </div>
  <div class="item">
    <div class="item-title">
      Section 2<i class="fa item-open-close fa-plus"></i>
    </div>
    <div class="item-body" style="display: none;">
      This is content. This is content. This is content. This is content. This is content. This is content. This is content.   
    </div>  
  </div>
  <div class="item">
    <div class="item-title">
      Section 3<i class="fa item-open-close fa-plus"></i>
    </div>
    <div class="item-body" style="display: none;">
      This is content. This is content. This is content. This is content. This is content. This is content. This is content.   
    </div>  
  </div>
</div>
</div>

希望这有帮助:)

bttbmeg0

bttbmeg03#

改进了@MayK的答案,以便可以关闭当前项目。

var acc = document.getElementsByClassName("accordion");
    var i, j;
    
    for (i = 0; i < acc.length; i++) {
      acc[i].addEventListener("click", function() {
        for (j = 0; j < acc.length; j++) {
        if(acc[j] != this) {
           acc[j].classList.remove("active");
           acc[j].nextElementSibling.style.maxHeight = null;
         }
        }
        if (!this.classList.contains("active")){
          this.classList.toggle("active");
          var panel = this.nextElementSibling;
          if (panel.style.maxHeight){
            panel.style.maxHeight = null;
          } else {
            panel.style.maxHeight = panel.scrollHeight + "px";
          } 
        }
    
      });
    }

相关问题