Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

              
                <div class="menu">
  <ul>
    <li><a href="Home">home</a></li>
    <li><p class="navItem">item 1</p>
      <ul class="subMenu subMenuOne">
        <h2>Sub Menu One</h2>
        <li><a href="sub item 1">sub item 1</a></li>
        <li><a href="sub item 2">sub item 2</a></li>
        <p class="close">Close</p>
      </ul>
    </li>
        <li><p class="navItem">item 2</p>
      <ul class="subMenu subMenuTwo">
        <h2>Sub Menu Two</h2>
        <li><a href="sub item 1">sub item 1</a></li>
        <li><a href="sub item 2">sub item 2</a></li>
        <p class="close">Close</p>
      </ul>
    </li>
        <li><p class="navItem">item 3</p>
      <ul class="subMenu subMenuThree">
        <h2>Sub Menu Three</h2>
        <li><a href="sub item 1">sub item 1</a></li>
        <li><a href="sub item 2">sub item 2</a></li>
        <p class="close">Close</p>
      </ul>
    </li>
  </ul>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  overflow: hidden;
}
.menu {
  padding: 3em;
  position: absolute;
  background: green;
  width: 30%;
  .subMenu {
    display: block;
    position: absolute;
    padding: 3em;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: #000;
    z-index: -99;
    color: white;
    width: 100%;
  }
  .subMenuOne {
    background: red;
  }
  .subMenuTwo {
    background: pink;
  }
  .subMenuThree {
    background: purple;
  }
}

li {
  padding: 1em;
}

@media only screen and (max-width: 799px) {
  .menu {
    width: 100%;
    .subMenu {
      z-index: 10;
      // transform: translateX(-100%);
    }
  }
}
              
            
!

JS

              
                let navItems = gsap.utils.toArray(".navItem"),
    openItem; // stores a reference to the currently open nav item (if there is one)

installMediaQueryWatcher("(max-width: 790px)", function(matches) {
  
  navItems.forEach(item => {
    item.removeEventListener("click", item.toggle);
    let closeButton = item.querySelector(".close");
    closeButton && closeButton.removeEventListener("click", item.close);
  });
  
  let startAt = matches ? -100 : 0,
      endAt = matches ? 0 : 100;
  
  gsap.set(".subMenu", {xPercent: startAt});
  if (openItem) {
    gsap.set(openItem.parentNode.querySelector(".subMenu"), {xPercent: endAt});
  }

  // loop through all .navItem elements to set up common functionality for them all...
  navItems.forEach(navItem => {
    let subMenu = navItem.parentNode.querySelector(".subMenu"),
        closeButton = subMenu.querySelector(".close"),
        isOpen = navItem === openItem;

    // add an "open()" method to the element itself for convenience
    navItem.open = () => {
      if (!isOpen) {
        isOpen = true;
        let tl = gsap.timeline();
        if (openItem) { // if there's an open nav item, close it first!
          tl.add(openItem.close());
        }
        console.log("animate to", endAt);
        tl.to(subMenu, {xPercent: endAt, overwrite: true});
        openItem = navItem; // store this element as the currently open one
        return tl;
      }
    };

    navItem.close = () => {
      if (isOpen) {
        isOpen = false;
        openItem = null;
        // return a tween because we'll insert that into a timeline in the calling method
        console.log("animate back to", startAt);
        return gsap.to(subMenu, {xPercent: startAt, overwrite: true});
      }
    };

    navItem.toggle = () => isOpen ? navItem.close() : navItem.open();

    // clicking on the nav text will toggle the menu
    navItem.addEventListener("click", navItem.toggle);
    closeButton.addEventListener("click", navItem.close);
  });
  
});

function installMediaQueryWatcher(mediaQuery, layoutChangedCallback) {
  var mql = window.matchMedia(mediaQuery);
  mql.addListener(function (e) { return layoutChangedCallback(e.matches); });
  layoutChangedCallback(mql.matches);
};
              
            
!
999px

Console