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

              
                <button id="btn"></button>
<nav id="menu">
  <ul class="globalNav">
    <li><a href="#">メニュー1</a></li>
    <li><span class="subNavBtn">メニュー2</span>
        <ul class="subNav">
          <li><a href="#">メニュー2-1</a></li>
          <li><a href="#">メニュー2-2</a></li>
          <li><a href="#">メニュー2-3</a></li>
        </ul>
    </li>
    <li><a href="#">メニュー3</a></li>
    <li><span class="subNavBtn">メニュー4</span>
        <ul class="subNav">
          <li><a href="#">メニュー4-1</a></li>
          <li><a href="#">メニュー4-2</a></li>
          <li><a href="#">メニュー4-3</a></li>
        </ul>
    </li>
  </ul>
</nav>

              
            
!

CSS

              
                #btn{
  position: relative;
  background: none;
  border: none;
  margin-bottom: 1em;
  
  &:before{
    content: '\f0c9';
    font-family: "Font Awesome 5 Free";
	font-weight: 900;
    font-size: 40px;
  }
  
  &.is-active:before{
    content: '\f00d';
  }
}

#menu{
  height: 0;
  overflow: hidden;

  .is-menuOpen &{
    height: auto;
  }
}


// サブナビゲーション
.subNavBtn{
  display: inline-block;
  
  &:after{
    content: '▼';
  }
  
  &.is-active:after {
    content: '▲';
  }
}

.subNav{
  height: 0;
  overflow: hidden;
  margin: 0;

  .subNavBtn.is-active + & {
    height: auto;
  }
}
              
            
!

JS

              
                const html = document.querySelector('html')
const btn = document.getElementById('btn')
const menu = document.getElementById('menu')

// 下層のメニューを閉じる処理の関数
function subMenuClose () {
  const opens = document.querySelectorAll('.subNavBtn.is-active')

  // IE対策
  const opensList = Array.prototype.slice.call(opens, 0)

  opensList.forEach(e => {
    e.classList.remove('is-active')
  })
}

// ハンバーガーメニューの開閉
btn.addEventListener('click', () => {
  btn.classList.toggle('is-active')
  if (btn.classList.contains('is-active')) {
    // 開く処理
    html.classList.add('is-menuOpen')
  } else {
    // 閉じる処理
    html.classList.remove('is-menuOpen')
    subMenuClose()
  }
})


// 下層のメニューの開閉
const subNavs = document.querySelectorAll('.subNavBtn')

// IE対策
const subNavsList = Array.prototype.slice.call(subNavs, 0)

subNavsList.forEach(e => {
  e.addEventListener('click', () => {
    e.classList.toggle('is-active')
  })
})


// メニューをクリックしたら閉じる
const links = document.querySelectorAll('.globalNav a')

// IE対策
const linksList = Array.prototype.slice.call(links, 0)
  linksList.forEach(e => {
    e.addEventListener('click', function () {
      const btn = document.getElementById('btn')
      btn.click()
    })
})
              
            
!
999px

Console