如何创建:可折叠内容/手风琴

Learn how to create an accordion (collapsible content).

Accordion

Accordion is very useful when you want to switch between hiding and showing a large amount of content:

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.

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.

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.

Coba sendiri

Create an accordion

第一步 - Add HTML:

<button class="accordion">Section 1</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>
<button class="accordion">Section 2</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>
<button class="accordion">Section 3</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>

第二步 - Add CSS:

Set the style of the accordion

/* Set style for the button used to open and close the accordion panel */
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  text-align: left;
  border: none;
  outline: none;
  transition: 0.4s;
}
/* If the button is clicked (add .active class using JS), add background color and when the mouse hovers over it (hover) */
.active, .accordion:hover {
  background-color: #ccc;
}
/* Add style to the accordion panel. Note: It is hidden by default */
.panel {
  padding: 0 18px;
  background-color: white;
  display: none;
  overflow: hidden;
}

第三步 - Add JavaScript:

var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    /* Toggle add and delete "active" class to highlight the control panel button */
    this.classList.toggle("active");
    /* Toggle hide and show active panel */
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {}}
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}

Coba sendiri

alat angklung animasi (geser ke bawah)

Untuk membuat alat angklung animasi, tambahkan max-height: 0danoverflow: hidden dan max-height efek transisi properti.

kemudian, gunakan JavaScript untuk menghitung dan menetapkan tinggi panel berdasarkan ukuran layar yang berbeda max-heightuntuk mencapai efek geser kandungan ke bawah:

<style>
.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
</style>
<script>
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";
    }
  });
}
</script>

Coba sendiri

Tambah ikon

Tambah simbol di setiap tombol untuk menunjukkan kandungan yang dapat dipegang atau ditutup:

.accordion:after {
  content: '\02795'; /* "tambah" (menambah) Unicode aksara */
  font-size: 13px;
  color: #777;
  float: right;
  margin-left: 5px;
}
.active:after {
  content: "\2796"; /* "menjangan" (menarik) Unicode aksara */
}

Coba sendiri