Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                  <ul class="acdmenu">
    <li aria-expanded="true"><a href="#">アコーディオン1</a>
      <ul>
        <li><a href="#">アコーディオン1の子1</a></li>
        <li><a href="#">アコーディオン1の子2</a></li>
        <li><a href="#">アコーディオン1の子3</a></li>
      </ul>
    </li>
    <li aria-expanded="false"><a href="#">アコーディオン2</a>
      <ul>
        <li><a href="#">アコーディオン2の子1</a></li>
        <li><a href="#">アコーディオン2の子2</a></li>
        <li><a href="#">アコーディオン2の子3</a></li>
      </ul>
    </li>
    <li aria-expanded="false"><a href="#">アコーディオン3</a>
      <ul>
        <li><a href="#">アコーディオン3の子1</a></li>
        <li><a href="#">アコーディオン3の子2</a></li>
        <li><a href="#">アコーディオン3の子3</a></li>
      </ul>
    </li>
  </ul>


              
            
!

CSS

              
                .acdmenu ul {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}

.acdmenu li[aria-expanded="true"] > ul {
  max-height: 1000px;
  transition: max-height 0.3s ease-out;
}


/* 装飾用 */
.acdmenu{
  margin:0;
  padding:0;
  background:#BE92A2;
  list-style:none;
  ul{
    margin:0;
    padding:0;
    list-style:none;
  }
    >li{
    >a{
      padding:10px 16px;
      &::before{
        content:"add";
        font-family: 'Material Icons';
        display:inline-block;
        transition: transform .2s ease-in-out;
      }
      &:hover{
        background:#DBBADD;
      }
    }
    li{
      >a{
        padding:8px 16px 8px 32px;
        &:hover{
          background:#96CDFF;
        }
      }
    }
  }
  li{
    a{
      display:block;
      color:#333;
      text-decoration:none;
      transition:all .2 ease-in-out;
    }
    ul{
      background:#D8E1FF;
      li{
        // padding-left:1em;
      }
    }
    &[aria-expanded="true"]{
      >a{
        &::before{
          transform: rotate(-405deg);
        }
      }
    }
  }
}
              
            
!

JS

              
                // クリックイベントをリッスンしてアコーディオンをトグルする
const accordionItems = document.querySelectorAll('.acdmenu > li');
const firstAccordionItem = accordionItems[0];

// 最初の li 要素を開く
firstAccordionItem.setAttribute('aria-expanded', true);
const firstUl = firstAccordionItem.querySelector('ul');
if (firstUl) {
  firstUl.style.display = 'block';
}

accordionItems.forEach(item => {
  item.addEventListener('click', event => {
    const currentItem = event.currentTarget;
    const isItemExpanded = currentItem.getAttribute('aria-expanded') === 'true';

    // すべてのアイテムを閉じる
    accordionItems.forEach(item => {
      item.setAttribute('aria-expanded', false);
      const ul = item.querySelector('ul');
      if (ul) {
        ul.style.display = 'none';
      }
    });

    // クリックされたアイテムを開く
    if (!isItemExpanded) {
      currentItem.setAttribute('aria-expanded', true);
      const ul = currentItem.querySelector('ul');
      if (ul) {
        ul.style.display = 'block';
      }
    }
  });
});

              
            
!
999px

Console