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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <body>
  <h1>Toggle Menu with the Animation API (Javascript)</h1>
  <p>The Animation API is an experimental Web API that provides animation controls, keyframes and a timeline to a DOM element or source.</p>
  <header>
  <div class="item--logo-wrapper">
    <div class="item item--logo">My website</div>
    <div class="item--trigger"><a href="#">Menu</a></div>
  </div>
  <div class="item item--menu">
    <nav>
      <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">Case Studies</a></li>
          <li><a href="#">Contact</a></li>
      </ul>
    </nav>
  </div>
</header>
<footer>
  <p><a href="https://www.miguel.nz/blog/toggle-menu-with-the-animation-api/" target="_blank">Read the full article here</a</p>
</footer>
</body>
              
            
!

CSS

              
                .item--menu
  height 0
  opacity 0

  
// *********************************
// Only for styles purposes
// *********************************
*
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;

h1
  color #fff
  font-weight normal
  padding-top 0.5em
  line-height 135%
body
  background #10AC81
  display flex
  align-items center
  flex-direction column
  max-width 550px
  margin 0 auto
  line-height 165%
  color white
header
  width 100%
  margin 0 auto
  color white
  margin-top 1em
  box-shadow 0 10px 20px -5px rgba(0,0,0,0.5)

 footer
   color #f1f1f1
   padding-top 2em
   font-size 0.85em
   position relative
   a
     background #fff
     position relative
     color #165286
     text-decoration none
     display inline-block
     padding 1em 2em
     border-radius 40px
     &:hover
       background #f1f1f1
     &:active
       top 1px
  
.item--menu
  background #fff
  overflow hidden
  ul
    list-style none
    margin 0
    padding 0
  li
    border-top 1px solid #fff
    border-bottom 1px solid #DDD
    &:last-child
      border-bottom 0 none
  a
    display block
    color #111
    text-decoration none
    padding 1em
    
.item--logo-wrapper
  display flex
  background #165286
  padding 1rem
  justify-content space-between
.item--logo
  font-weight bold
  font-size 1.2em
.item--trigger a
  color white
  text-decoration none
  text-transform uppercase
  font-size 0.85em
              
            
!

JS

              
                class headerMenu { 
  constructor() {
    // The trigger and menu wrapper
    this.triggerMenu = document.querySelector('.item--trigger a');
    this.menuWrapper = document.querySelector('.item--menu');

    // The event Listener
    this.toggleMenu = this.toggleMenu.bind(this);
    this.triggerMenu.addEventListener('click', this.toggleMenu);

    // The animation
    this.openMenuAnimation = new Animation(this.menuKeyFrames({
      duration: 200, 
      fill: 'forwards' 
    }));

    this.closeMenuAnimation = new Animation(this.menuKeyFrames({
      duration: 200, 
      fill: 'forwards',
      direction: 'reverse'
    }));
  }

  menuKeyFrames(options) {
    const menuNavHeight = parseInt(this.menuWrapper.querySelector('nav').getBoundingClientRect().height);
    
    return new KeyframeEffect( 
      this.menuWrapper,
      [{ 
          height: 0,
          opacity: 0,
        },
        { 
          height: `${menuNavHeight}px`,
          opacity: 1
        }], 
      options 
    );
  }

  toggleMenu(event) {
    event.preventDefault();
    if (!this.triggerMenu.classList.contains('active')) {
      this.triggerMenu.classList.add('active');
      this.closeMenuAnimation.cancel();
      this.openMenuAnimation.play();
    } else {
      this.triggerMenu.classList.remove('active');
      this.openMenuAnimation.cancel();
      this.closeMenuAnimation.play();
    }
  }
}

new headerMenu();
              
            
!
999px

Console