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

              
                <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="#">Click Me</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: #fff;
}
header {
	width: 100%;
	margin: 0 auto;
	color: #fff;
	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: #fff;
		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