0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ハンバーガーメニューの完全なコードはこちらから。

Posted at

ハンバーガーメニューを自作するのが面倒な人へ捧ぐ。

HTML

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>hamburger practice</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <nav class="nav">
      <button class="hamburger">
        <span></span>
        <span></span>
        <span></span>
      </button>
      <div class="menu">
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About us</a></li>
          <li><a href="#service">Service</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </div>
    </nav>

    <script src="./script.js"></script>
  </body>
</html>

CSS

/* リセットCSS */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  padding: 1rem;
  background-color: #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.hamburger {
  position: relative;
  width: 30px;
  height: 24px;
  cursor: pointer;
  z-index: 100;
  border: none;
  background: none;
}

.hamburger span {
  position: absolute;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #333;
  transition: all 0.4s;
}

.hamburger span:nth-child(1) {
  top: 0;
}

.hamburger span:nth-child(2) {
  top: 11px;
}

.hamburger span:nth-child(3) {
  bottom: 0;
}

.hamburger.active span:nth-child(1) {
  transform: translateY(11px) rotate(-45deg);
  background-color: #fff;
}

.hamburger.active span:nth-child(2) {
  opacity: 0;
  background-color: #fff;
}

.hamburger.active span:nth-child(3) {
  transform: translateY(-11px) rotate(45deg);
  background-color: #fff;
}

.menu {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-color: rgba(0,0,0,0.9);
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transform: translateX(100%);
  transition: transform 0.4s;
}

.menu.active {
  transform: translateX(0);
}

.menu ul {
  list-style: none;
}

.menu li {
  margin: 1.5rem 0;
  font-size: 1.5rem;
  opacity: 0;
  transform: translateX(20px);
  transition: all 0.4s;
}

.menu.active li {
  opacity: 1;
  transform: translateX(0);
}

/* メニュー項目の遅延アニメーション */
.menu.active li:nth-child(1) { transition-delay: 0.2s; }
.menu.active li:nth-child(2) { transition-delay: 0.3s; }
.menu.active li:nth-child(3) { transition-delay: 0.4s; }
.menu.active li:nth-child(4) { transition-delay: 0.5s; }

/* メニューリンク */
.menu a {
  color: #fff;
  text-decoration: none;
  position: relative;
}

.menu a::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #fff;
  transform: scaleX(0);
  transition: transform 0.3s;
  transform-origin: right;
}

.menu a:hover::after {
  transform: scaleX(1);
  transform-origin: left;
}

JavaScript

const hamburger = document.querySelector(".hamburger");
const menu = document.querySelector(".menu");

hamburger.addEventListener("click", () => {
  hamburger.classList.toggle("active");
  menu.classList.toggle("active");
});

document.querySelectorAll(".menu a").forEach((link) => {
  link.addEventListener("click", () => {
    hamburger.classList.remove("active");
    menu.classList.remove("active");
  });
});

document.addEventListener("click", (e) => {
  if (!hamburger.contains(e.target) && !menu.contains(e.target)) {
    hamburger.classList.remove("active");
    menu.classList.remove("active");
  }
});
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?