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

Save Automatically?

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="include-accordion scroll-control">
  <li>
    <button class="accordionBtn" type="button">Menu-1</button>
    <ul>
      <li>List 1-1</li>
      <li>List 1-2</li>
      <li>List 1-3</li>
    </ul>
  </li>
  <li>
    <button class="accordionBtn" type="button">Menu-2</button>
    <ul>
      <li>List 2-1</li>
      <li>List 2-2</li>
      <li>List 2-3</li>
    </ul>
  </li>
  <li>
    <button class="accordionBtn" type="button">Menu-3</button>
    <ul>
      <li>List 3-1</li>
      <li>List 3-2</li>
      <li>List 3-3</li>
    </ul>
  </li>
</ul>
  
<ul class="include-accordion scroll-control">
  <li>
    <button class="accordionBtn" type="button">Menu-1</button>
    <ul>
      <li>List 1-1</li>
      <li>List 1-2</li>
      <li>List 1-3</li>
    </ul>
  </li>
  <li>
    <button class="accordionBtn" type="button">Menu-2</button>
    <ul>
      <li>List 2-1</li>
      <li>List 2-2</li>
      <li>List 2-3</li>
    </ul>
  </li>
  <li>
    <button class="accordionBtn" type="button">Menu-3</button>
    <ul>
      <li>List 3-1</li>
      <li>List 3-2</li>
      <li>List 3-3</li>
    </ul>
  </li>
</ul>
              
            
!

CSS

              
                ul{
  background-color: #35924A;
  width: 150px;
  padding: 0;
  color: #fff;
  float: left;
  margin-left:30px;
}

li{
  list-style: none;
}

ul ul{
  height: 0;
  padding: 0;
  overflow: hidden;
  transition: .5s;
  border-top: 1px solid #67a863;
  background-color: #5EAA6C;
  margin:0;
}

ul li li{
  border-bottom: 1px dotted #7FBF8B;
  padding: 10px 0 10px 10px;
  margin-left:15px;
}

ul:nth-of-type(1) li.active li:last-child{
  border-bottom:1px solid #67a863; 
}

button{
  position: relative;
  border: none;
  width: 100%;
  background-color: inherit;
  color: #fff;
  cursor: pointer;
  text-align: left;
  padding: 15px 0 15px 20px;
  font-size:1em;
}
button:hover{
  background-color: #1A5B27;
}

button::before,
button::after{
  content:"";
  position: absolute;
  top: 20px;
  width: 1.5px;
  height: 8px;
  background-color: #fff;
  transition: .5s;
}

button::before{
  transform: rotate(-45deg);
  right: 35px;
}

button::after{
  transform: rotate(45deg);
  right: 30px;
}

li.active button::before{
  transform: rotate(-135deg);
  transition:.5s;
}

li.active button::after{
  transform: rotate(135deg);
  transition:.5s;
}

ul:nth-of-type(2){ background-color: #357D87; }
ul:nth-of-type(2) ul{ background-color: #519FA5; border-top: 1px solid #5D9FA8; }
ul:nth-of-type(2) button:hover{ background-color: #1C4B56; }
ul:nth-of-type(2) li li{ border-bottom: 1px dotted #73BEBF; }
ul:nth-of-type(2) li.active li:last-child{ border-bottom:1px solid #5D9FA8; }

ul.active{ overflow-y: auto; }
              
            
!

JS

              
                // メニューを開く関数
const slideDown = (el) => {
  el.style.height = 'auto'; //いったんautoに
  let h = el.offsetHeight; //autoにした要素から高さを取得
  el.style.height = h + 'px';
  el.animate([ //高さ0から取得した高さまでのアニメーション
    { height: 0 },
    { height: h + 'px' }
  ], {
    duration: 300, //アニメーションの時間(ms)
   });
};

// メニューを閉じる関数
const slideUp = (el) => {
  el.style.height = 0;
};

let activeIndex = null; //開いているアコーディオンのindex

//アコーディオンコンテナ全てで実行
const accordions = document.querySelectorAll('ul.include-accordion');
accordions.forEach((accordion) => {

  //アコーディオンボタン全てで実行
  const accordionBtns = accordion.querySelectorAll('.accordionBtn');
  accordionBtns.forEach((accordionBtn, index) => {
    accordionBtn.addEventListener('click', (e) => {
      activeIndex = index; //クリックされたボタンを把握
      e.target.parentNode.classList.toggle('active'); //ボタンの親要素(=ul>li)にクラスを付与/削除
      const content = accordionBtn.nextElementSibling; //ボタンの次の要素(=ul>ul)
      if(e.target.parentNode.classList.contains('active')){
        slideDown(content); //クラス名がactive(=閉じていた)なら上記で定義した開く関数を実行
      }else{
        slideUp(content); //クラス名にactiveがない(=開いていた)なら上記で定義した閉じる関数を実行
      }
      accordionBtns.forEach((accordionBtn, index) => {
        if (activeIndex !== index) {
          accordionBtn.parentNode.classList.remove('active');
          const openedContent = accordionBtn.nextElementSibling;
          slideUp(openedContent); //現在開いている他のメニューを閉じる
        }
      });
      //スクロール制御のために上位階層ulのクラス名を変える
      let container = accordion.closest('.scroll-control'); //sroll-controlnのクラス名である親要素を取得
      if(accordionBtn.parentNode.classList.contains('active') == false && container !== null ){
        container.classList.remove('active')
      }else if (container !== null){
        container.classList.add('active')
      }
    });
  });
});
              
            
!
999px

Console