0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UI:アコーディオンカーテン(バニラJS+HTML/CSS)

Posted at
<div class="accordion">
  <div class="accordion-item">
    <button class="accordion-header">見出し1</button>
    <div class="accordion-content">
      <label><input type="checkbox"> チェックボックス1</label>
      <label><input type="checkbox"> チェックボックス2</label>
      <label><input type="checkbox"> チェックボックス3</label>
    </div>
  </div>

  <div class="accordion-item">
    <button class="accordion-header">見出し2</button>
    <div class="accordion-content">
      <label><input type="checkbox"> チェックボックス4</label>
      <label><input type="checkbox"> チェックボックス5</label>
    </div>
  </div>
</div>
document.querySelectorAll('.accordion-header').forEach(button => {
  button.addEventListener('click', function () {
    const content = this.nextElementSibling;
    const isOpen = content.style.maxHeight;

    // 他のアコーディオンを閉じる場合
    document.querySelectorAll('.accordion-content').forEach(item => {
      if (item !== content) {
        item.style.maxHeight = null;
      }
    });

    // 開閉の切り替え
    content.style.maxHeight = isOpen ? null : content.scrollHeight + 'px';
  });
});
.accordion-header {
  width: 100%;
  background-color: #007bff;
  color: white;
  padding: 10px;
  border: none;
  text-align: left;
  cursor: pointer;
  font-size: 16px;
}

.accordion-content {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.3s ease-out;
  padding-left: 10px;
}

.accordion-item {
  border-bottom: 1px solid #ccc;
}

label {
  display: block;
  margin: 5px 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?