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="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 = function(el) {
  el.style.height = 'auto'; //いったんautoに
  let h = el.offsetHeight;  //autoにした要素から高さを取得
  el.animate({ // 高さ0から取得した高さまでのアニメーション
    height: [ 0, h + 'px' ]
  }, {
    duration: 300, // アニメーションの時間
   });
   el.style.height = 'auto';  //ブラウザの表示幅を途中で閲覧者が変えた時を考慮してautoに戻す
   el.setAttribute('aria-hidden', 'false');  //WAI-ARIA対応、閉じた状態であることを支援技術に伝える
};

// 要素を非表示にする関数
const slideUp = function(el) {
  let h = el.offsetHeight;
  el.style.height = h + 'px';
  el.animate({
    height: [ h + 'px', 0]
  }, {
    duration: 300,
   });
   el.style.height = 0;
   el.setAttribute('aria-hidden', 'true');  //WAI-ARIA対応、開いた状態であることを支援技術に伝える
};

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

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

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

Console