【コピペ可】ハンバーガーメニューの作り方 / CSS.HYML.JavaScript

Contents

ハンバーガーメニューとは

ハンバーガーメニューは、 Webの画面上部の隅に配置される押しボタンのことを指します。

形がハンバーガーに似ていることから、そのように名付けられたアイコンです。

この記事で記述されているコードは下記の3つになります。

  1. メニューをクリックすると、三本線が✗に変形する
  2. メニューをクリックすると、隠れたメニューが出てくる
  3. ✗をクリックすると、✗が三本線に変形する

プレビュー

See the Pen hmenu by 古澤 良汰 (@ryotarofu) on CodePen.

【コピペOK】各コード紹介

HTML

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <header>
        <div class="right">
            <div class="toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
    <nav class="hmenu">
        <ul class="list">
            <li class="item"><a>HOME</a></li>
            <li class="item"><a>WORKS</a></li>
            <li class="item"><a>ABOUT</a></li>
            <li class="item"><a>CONTACT</a></li>
        </ul>
    </nav>
    </div>
    </header>
</body>
<script src="hmenu.js"></script>
</html>

CSS

@charset "utf-8";

header{
    margin: 0 auto;
    width: 90%;
}

body{
    background:#d0d0d0;
      padding:20px;
  }
  
  
a{
    color: #333;
    text-decoration: none;
  }

.right{
    display: flex;
    flex-direction: row-reverse;
}

.toggle{
    position: relative;
    cursor: pointer;
      width: 50px;
      height:50px;
    border-radius: 5px;
  }

  .toggle span{
      display: inline-block;
      transition: all .4s;
      position: absolute;
      height: 3px;
      left: 14px;
      border-radius: 2px;
      background: #fff;
      width: 45%;
      z-index: 9999;
    }
  
  .toggle span:nth-of-type(1) {
    top:15px; 
  }
  
  .toggle span:nth-of-type(2) {
    top:23px;
  }
  
  .toggle span:nth-of-type(3) {
    top:31px;
  }
  
  .toggle.active span:nth-of-type(1) {
      top: 18px;
      left: 18px;
      transform: translateY(6px) rotate(-45deg);
      width: 30%;
  }
  
  .toggle.active span:nth-of-type(2) {
    opacity: 0;
  }
  
  .toggle.active span:nth-of-type(3){
      top: 30px;
      left: 18px;
      transform: translateY(-6px) rotate(45deg);
      width: 30%;
  }

.hmenu {
    display: block;
    position: fixed;
    text-align: center;
    background-color: #000;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    padding: 30% 0;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.5s, visibility 0.5s;
  }

.list a {
    color: #fff;
  }

.list a:hover {
    color: #F7C242;
  }

.item {
    padding-bottom: 15px;
    padding-top: 15px;
    list-style: none;
  }

.hmenu.show {
    opacity: 1;
    width: 100%;
    height: 100%;
    z-index: 99;
    background-color: #444;
    transform: translate(-100% 100%);
    visibility: visible;
  }

JavaScript

let barAll = document.querySelector(".toggle");
let menu = document.querySelector(".hmenu");

barAll.addEventListener("click", function () {
    barAll.classList.toggle("active");
    menu.classList.toggle("show");
});

まとめ

上記のコードを自分の好きな形にカスタマイズして、制作しているWebサイトのデザインに合うハンバーガーメニューを制作していきましょう。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA