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>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기