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

              
                <nav class="menu js-menu">
    <div class="menu__contents js-menu-contents">
      <button class="menu__toggle js-menu-toggle">
        <h1 class="menu__title js-menu-title">Menu</h1>
      </button>
      <ul class="menu__items">
        <li class="menu__item submenu_container">Menu item 1
        <ul class="submenu">
          <li class="menu__item">sub menu </li>
          <li class="menu__item">sub menu </li>
          <li class="menu__item">sub menu </li>
         </ul>
        </li>
        <li class="menu__item">Menu item 2</li>
        <li class="menu__item">Menu item 3</li>
        <li class="menu__item">Menu item 4</li>
      </ul>
    </div>
  </nav>
              
            
!

CSS

              
                html, body {
      background: #666;
      font-family: Arial, Helvetica, sans-serif;
    }
    .menu {
      position: absolute;
      opacity: 0;
      pointer-events: none;
      transform-origin: top left;
      overflow: hidden;
      border-radius: 3px;
      box-shadow: 0 3px 3px rgba(0, 0, 0, 0.4);
      background: #FFF;
      will-change: transform;
      padding:.5em;
    }

.submenu{
  display:block;
  max-height:0em;
  opacity:0;
   -webkit-transition: all .5s ease-in-out;
  -moz-transition: all .5s ease-in-out;
  -o-transition: all .5s ease-in-out;
  transition: all .5s ease-in-out;
  padding-left:0px;
  background-color:#ccc;
  text-align:left;
}

.submenu li{
  list-style:none;
  color:#fff;
}

.submenu_container{
  padding:0px;
}

.submenu_container::after{
  content:">";
  position: absolute;
  right: 0px;
  top: .6em;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  transform: rotate(0deg);
   -webkit-transition: all .5s ease-in-out;
  -moz-transition: all .5s ease-in-out;
  -o-transition: all .5s ease-in-out;
  transition: all .5s ease-in-out;
}

.submenu_container.submenu_container__open::after{
   -webkit-transform: rotate(90deg);
  -moz-transform: rotate(90deg);
  -o-transform: rotate(90deg);
  -ms-transform: rotate(90deg);
  transform: rotate(90deg);
}

.submenu__open{
  display:block;
  opacity:1;
  max-height:40em;
  padding-top:.5em;
  padding-bottom:1em;
}

    .menu__contents {
      transform-origin: top left;
      will-change: transform;
    }
    .menu__toggle {
      text-align: left;
      padding: 0;
      margin: 0;
      border: none;
      background: none;
      cursor: pointer;
      width: 100%;
    }
    .menu__title {
      padding: 0;
      margin: 0;
      font-weight: 500;
      display: inline-block;
      padding: 8px 12px;
    }
    .menu__items {
      position: relative;
      list-style: none;
      padding: 0;
      margin: 0;
      background: #FFF;
      z-index: 1;
    }
    .menu__item {
      padding: 8px 12px;
      border-top: 1px solid #DDD;
    }
    .menu--active {
      opacity: 1;
      pointer-events: auto;
    }
    .menu--expanded {
      animation-name: menuExpandAnimation;
      animation-duration: 0.2s;
      animation-timing-function: linear;
    }
    .menu__contents--expanded {
      animation-name: menuExpandContentsAnimation;
      animation-duration: 0.2s;
      animation-timing-function: linear;
    }
    .menu--collapsed {
      animation-name: menuCollapseAnimation;
      animation-duration: 0.2s;
      animation-timing-function: linear;
    }
    .menu__contents--collapsed {
      animation-name: menuCollapseContentsAnimation;
      animation-duration: 0.2s;
      animation-timing-function: linear;
    }


              
            
!

JS

              
                /**
 *
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

class Menu {
  constructor () {
    this._menu = document.querySelector('.js-menu');
    this._menuContents = this._menu.querySelector('.js-menu-contents');
    this._menuToggleButton = this._menu.querySelector('.js-menu-toggle');
    this._menuTitle = this._menu.querySelector('.js-menu-title');

    this._expanded = true;
    this._animate = false;
    this._collapsed;

    this.expand = this.expand.bind(this);
    this.collapse = this.collapse.bind(this);
    this.toggle = this.toggle.bind(this);

    this._calculateScales();
    this._createEaseAnimations();
    this._addEventListeners();

    this.collapse();
    this.activate();
  }

  activate () {
    this._menu.classList.add('menu--active');
    this._animate = true;
  }

  collapse () {
    if (!this._expanded) {
      return;
    }
    this._expanded = false;

    const {x, y} = this._collapsed;
    const invX = 1 / x;
    const invY = 1 / y;

    this._menu.style.transform = `scale(${x}, ${y})`;
    this._menuContents.style.transform = `scale(${invX}, ${invY})`;
    
    if (!this._animate) {
      return;
    }

    this._applyAnimation({expand: false});
  }

  expand () {
    if (this._expanded) {
      return;
    }
    this._expanded = true;

    this._menu.style.transform = `scale(1, 1)`;
    this._menuContents.style.transform = `scale(1, 1)`;
      
    if (!this._animate) {
      return;
    }

    this._applyAnimation({expand: true});
  }

  toggle () {
    if (this._expanded) {
      this.collapse();
      return;
    }

    this.expand();
  }

  _addEventListeners () {
    this._menuToggleButton.addEventListener('click', this.toggle);
  }

  _applyAnimation ({expand}=opts) {
    this._menu.classList.remove('menu--expanded');
    this._menu.classList.remove('menu--collapsed');
    this._menuContents.classList.remove('menu__contents--expanded');
    this._menuContents.classList.remove('menu__contents--collapsed');

    // Force a recalc styles here so the classes take hold.
    window.getComputedStyle(this._menu).transform;

    if (expand) {
      this._menu.classList.add('menu--expanded');
      this._menuContents.classList.add('menu__contents--expanded');
      return;
    }

    this._menu.classList.add('menu--collapsed');
    this._menuContents.classList.add('menu__contents--collapsed');
  }

  _calculateScales () {
    const collapsed = this._menuTitle.getBoundingClientRect();
    const expanded = this._menu.getBoundingClientRect();

    this._collapsed = {
      x: collapsed.width / expanded.width,
      y: collapsed.height / expanded.height
    }
  }

  _createEaseAnimations () {
    let menuEase = document.querySelector('.menu-ease');
    if (menuEase) {
      return menuEase;
    }

    menuEase = document.createElement('style');
    menuEase.classList.add('menu-ease');

    const menuExpandAnimation = [];
    const menuExpandContentsAnimation = [];
    const menuCollapseAnimation = [];
    const menuCollapseContentsAnimation = [];
    for (let i = 0; i <= 100; i++) {
      const step = this._ease(i/100);
      const startX = this._collapsed.x;
      const startY = this._collapsed.y;
      const endX = 1;
      const endY = 1;

      // Expand animation.
      this._append({
        i,
        step,
        startX: this._collapsed.x,
        startY: this._collapsed.y,
        endX: 1,
        endY: 1,
        outerAnimation: menuExpandAnimation,
        innerAnimation: menuExpandContentsAnimation
      });

      // Collapse animation.
      this._append({
        i,
        step,
        startX: 1,
        startY: 1,
        endX: this._collapsed.x,
        endY: this._collapsed.y,
        outerAnimation: menuCollapseAnimation,
        innerAnimation: menuCollapseContentsAnimation
      });
    }

    menuEase.textContent = `
      @keyframes menuExpandAnimation {
        ${menuExpandAnimation.join('')}
      }
      @keyframes menuExpandContentsAnimation {
        ${menuExpandContentsAnimation.join('')}
      }
      
      @keyframes menuCollapseAnimation {
        ${menuCollapseAnimation.join('')}
      }
      @keyframes menuCollapseContentsAnimation {
        ${menuCollapseContentsAnimation.join('')}
      }`;

    document.head.appendChild(menuEase);
    return menuEase;
  }

  _append ({
        i,
        step,
        startX, 
        startY, 
        endX, 
        endY, 
        outerAnimation, 
        innerAnimation}=opts) {

    const xScale = startX + (endX - startX) * step;
    const yScale = startY + (endY - startY) * step;

    const invScaleX = 1 / xScale;
    const invScaleY = 1 / yScale;

    outerAnimation.push(`
      ${i}% {
        transform: scale(${xScale}, ${yScale});
      }`);

    innerAnimation.push(`
      ${i}% {
        transform: scale(${invScaleX}, ${invScaleY});
      }`);
  }

  _clamp (value, min, max) {
    return Math.max(min, Math.min(max, value));
  }

  _ease (v, pow=4) {
    v = this._clamp(v, 0, 1);

    return 1 - Math.pow(1 - v, pow);
  }
}

new Menu()

$( document ).ready(function() {
    $( ".submenu_container" ).click(function() {
      $(this).toggleClass( "submenu_container__open" );
      $(this).find('.submenu').toggleClass( "submenu__open" );
    });
});
              
            
!
999px

Console