Hamburger Menu Icon

HTML, CSS, JS: 간단한 확장형 햄버거 메뉴 만들기

“확장형 햄버거 메뉴는 어떻게 만들까요?”

데모: 이렇게 보입니다

아이콘을 클릭하면 메뉴가 열립니다. 링크를 누르면 아래 코드로 이동합니다.

HTML

메뉴 아이콘은 실제 <button> 요소이므로 키보드로 조작할 수 있고, 메뉴가 열려 있는지 스크린 리더에 알려 줍니다(aria-expanded). 각 링크는 열어야 할 섹션이나 페이지를 가리키도록 수정하세요.

<header class="header">
  <div class="header__menu menu">
    <button class="menu__icon" type="button" aria-label="Open menu" aria-expanded="false" aria-controls="menu-body">
      <span></span>
    </button>
    <nav class="menu__body" id="menu-body" aria-label="Main menu">
      <ul class="menu__list">
        <li><a href="#home" class="menu__link">Home</a></li>
        <li><a href="#section-1" class="menu__link">Section 1</a></li>
        <li><a href="#section-2" class="menu__link">Section 2</a></li>
        <li><a href="#section-3" class="menu__link">Section 3</a></li>
      </ul>
    </nav>
  </div>
</header>

CSS

디자인에 맞게 색상을 바꾸세요. 메뉴는 오른쪽에서 밀려 들어오며, 열리기 전에는 키보드로 접근할 수 없습니다.

.header__menu {
    position: fixed;
    top: 34px;
    right: 30px;
    z-index: 10;
}
.menu__icon {
    position: relative;
    z-index: 5;
    display: block;
    width: 50px;
    height: 22px;
    padding: 0;
    border: 0;
    background: none;
    cursor: pointer;
}
.menu__icon span,
.menu__icon::before,
.menu__icon::after {
    position: absolute;
    left: 0;
    width: 100%;
    height: 20%;
    background-color: #111;
    transition: all 0.3s ease 0s;
}
.menu__icon::before,
.menu__icon::after {
    content: '';
}
.menu__icon::before {
    top: 0;
}
.menu__icon::after {
    bottom: 0;
}
.menu__icon span {
    top: 50%;
    transform: scale(1) translate(0, -50%);
}
.menu__icon._active span {
    transform: scale(0) translate(0, -50%);
}
.menu__icon._active::before {
    top: 50%;
    transform: rotate(-45deg) translate(0, -50%);
}
.menu__icon._active::after {
    bottom: 50%;
    transform: rotate(45deg) translate(0, 50%);
}
.menu__body {
    position: fixed;
    top: 0;
    right: -100%;
    width: 34%;
    min-width: 240px;
    height: 100%;
    padding: 100px 30px 30px;
    box-sizing: border-box;
    overflow: auto;
    background-color: rgba(255, 255, 255, 0.95);
    visibility: hidden;
    transition: right 0.3s ease 0s, visibility 0.3s ease 0s;
}
.menu__body._active {
    right: 0;
    visibility: visible;
}
.menu__list {
    margin: 0;
    padding-left: 1rem;
    list-style: none;
}
.menu__list li {
    margin-bottom: 30px;
}
.menu__link {
    display: block;
    color: #111;
    font-size: 2.2rem;
    font-weight: 200;
    line-height: 1.2;
    text-decoration: none;
}

JavaScript

외부 파일은 필요 없습니다. 이 스크립트를 닫는 </body> 태그 바로 앞(또는 테마의 푸터 스크립트 영역)에 붙여 넣으세요. 메뉴를 열고 닫으며, 링크를 선택하거나 Esc 키를 누르면 메뉴가 다시 닫힙니다.

<script>
document.addEventListener('DOMContentLoaded', function () {
  var button = document.querySelector('.menu__icon');
  var menu = document.querySelector('.menu__body');
  if (!button || !menu) return;

  function setMenu(open) {
    button.classList.toggle('_active', open);
    menu.classList.toggle('_active', open);
    button.setAttribute('aria-expanded', open ? 'true' : 'false');
    button.setAttribute('aria-label', open ? 'Close menu' : 'Open menu');
  }

  button.addEventListener('click', function () {
    setMenu(button.getAttribute('aria-expanded') !== 'true');
  });

  // Close the menu when a link is chosen or Escape is pressed
  menu.addEventListener('click', function (e) {
    if (e.target.closest('a')) setMenu(false);
  });
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && button.getAttribute('aria-expanded') === 'true') {
      setMenu(false);
      button.focus();
    }
  });
});
</script>

내비게이션에 햄버거 메뉴를 넣으면 웹사이트가 한층 세련되고 깔끔해 보입니다.

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다