AMP
  • websites

Animated hamburger menu

Introduction

Small interactive UI animations communicate state and purpose to users. A common case is a hamburger menu button that animates into an "X" when open. This animation communicates that the user can press the same button again to close the menu.

CSS animations

Below is the custom styles and CSS animations for the hamburger menu:

<style amp-custom>
  .hamburger_wrapper {
    padding: 5px;
    z-index: 10;
  }

  #hamburger {
    width: 60px;
    height: 45px;
    position: relative;
    cursor: pointer;
    outline: none;

  }

  #hamburger span {
    display: block;
    position: absolute;
    height: 9px;
    width: 100%;
    background: #005af0;
    border-radius: 9px;
    opacity: 1;
    left: 0;
    transform: rotate(0deg);
    transition: .5s ease-in-out;
  }

  #hamburger span:nth-child(1) {
    top: 0px;
    transform-origin: left center;
  }

  #hamburger span:nth-child(2) {
    top: 21px;
    transform-origin: left center;
  }

  #hamburger span:nth-child(3) {
    top: 42px;
    transform-origin: left center;
  }

  #hamburger.close span:nth-child(1) {
    transform: rotate(45deg);
  }

  #hamburger.close span:nth-child(2) {
    width: 0%;
    opacity: 0;
    transition: .1s;
  }

  #hamburger.close span:nth-child(3) {
    transform: rotate(-45deg);
  }

  #nav-menu {
    position: relative;
    transform: translateX(-100vw);
    opacity: 0;
    z-index: 10;
    transition: transform .5s ease, opacity ease .2s;
  }

  #nav-menu.now-active {
    transform: translateX(0);
    transition: transform .5s ease, opacity ease .2s;
    opacity: 1;
    background-color: #eaeaea;
  }

  .nav-list {
    padding: 10px;
    list-style-type: none;
    font-size: 2em;
  }

</style>

Markup

The hamburger menu element contains three styled span elements inside a div. The hamburger div uses the tap AMP action to trigger the toggleClass() event, which targets an element by its id. In this example, it targets hamburger.

The toggleClass() event will add and remove any class it's given as an argument. This example passes class='close'. Additionally, it toggles an animation class for the navigation menu to slide out.

<div class="hamburger_wrapper">
  <div id="hamburger" tabindex="1" role="button" on="tap:hamburger.toggleClass(class='close'),nav-menu.toggleClass(class='now-active')">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<div id="nav-menu">
  <ul class="nav-list">
    <li><a href="#">Home</a></li>
    <li><a href="#">Account</a></li>
    <li><a href="#">Products</a></li>
    <li><a href="#">Support</a></li>
  </ul>
</div>
Besoin de plus amples explications ?

Si les explications de cette page ne répondent pas à vos questions, n'hésitez pas à contacter d'autres utilisateurs d'AMP pour discuter de votre cas d'utilisation spécifique.

Se rendre sur Stack Overflow
Une fonctionnalité n'a pas encore été expliquée ?

Le projet AMP encourage fortement votre participation et vos contributions ! Nous espérons que vous deviendrez un membre régulier de notre communauté open source, mais nous serons également ravis de recevoir des contributions ponctuelles concernant les questions qui vous intéressent particulièrement.

Modifier l'exemple sur GitHub