【CSS/jQuery】左からハンバーガーメニューをアニメーションで表示させる方法

CSS

実務のコピペ用。

HTML

<!-- ハンバーガーボタン -->
<div id="nav-toggle">
  <div>
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<!-- グローバルナビ ハンバーガーメニュー -->
<div id="gloval-nav">
  <nav>
    <ul>
      <li><a href="">メニュー</a></li>
      <li><a href="">メニュー</a></li>
      <li><a href="">メニュー</a></li>
      <li><a href="">メニュー</a></li>
    </ul>
  </nav>
</div>

CSS

#nav-toggle {
  position: fixed;
  top: 20px;
  right: 20px;
  cursor: pointer;
  display: none;
  @include sp {
      display: block;
  }
  > div {
    position: relative;
    width: 20px;
  }
  span {
    width: 100%;
    height: 1px;
    left: 0;
    display: block;
    background: black;
    position: absolute;
    transition: transform .6s ease-in-out, top .5s ease;
    
    &:nth-child(1) {
      top: 0;
    }
    &:nth-child(2) {
        top: 7px;
    }
    &:nth-child(3) {
        top: 14px;
    }
  }
}

.open {
  #nav-toggle span {
    background: #fff;
    &:nth-child(1) {
        top: 7px;
        transform: rotate(45deg);
    }
    &:nth-child(2) {
        top: 14px;
        width: 0;
        left: 50%;
    }
    &:nth-child(3) {
        top: 7px;
      transform: rotate(-45deg);
    }
  }
}

/* z-index */
#nav-toggle {
  z-index: 1000;
}

#gloval-nav {
  background-color: pink;
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 990;
  text-align: center;
  display: flex;
//   visibility: hidden;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 2.5rem;
  transform: translateX(-100%);
  transition: opacity .6s ease, visibility .6s ease;
}


#gloval-nav {
  a {
    display: block;
    color: #fff;
    padding: 20px 0;
    transition: color .6s ease;
  }
  ul {
    li {
      opacity: 0;
      transform: translateX(-200px);
      transition:  transform .6s ease, opacity .2s ease;
      &:nth-child(2) {
        transition-delay: .15s;
      }
      &:nth-child(3) {
        transition-delay: .3s;
      }
      &:nth-child(4) {
        transition-delay: .45s;
      }
    }
  }
} 

/* open */
.open {
  overflow: hidden;
  
  #gloval-nav {
    visibility: visible;
    transform: translateX(0);
    transition: transform .6s;
  }
  #gloval-nav li {
    opacity: 1;
    transform: translateX(0);
    transition:  transform 1s ease, opacity .9s ease;
  }
}

jQuery

CDNコードの記述も忘れずに。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

jQueryのコードはこちら。

(function ($) {
    $(function () {
        $('#nav-toggle').on('click', function () {
            $('body').toggleClass('open');
        });
    });
})(jQuery);