Web白描

Webデザインの勉強 - 演習

ハンバーガーメニュー(jQuery)

ハンバーガーメニュー(jQuery

完成例
  • スクロールしても「ハンバーガーメニュー」は固定の状態にする

記述例

index.html

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>【コーディング練習】ハンバーガーメニュー</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js" defer></script>
<script src="js/script.js" defer></script>
</head>
<body>

<!-- ▼ header -->
<header id="header">
  <h1>ハンバーガーメニュー</h1>
  <nav class="gnav">
    <ul>
      <li><a href="#">TOP</a></li>
      <li><a href="#">ABOUT</a></li>
      <li><a href="#">SERVICE</a></li>
    </ul>
  </nav>
  <div id="hmb">
    <span></span>
    <span></span>
    <span></span>
  </div><!-- /#hmb -->
  <div id="mask"></div>
</header>
<!-- ▲ header -->

<!-- ▼ main -->
<main class="main">
  ここにmainの内容が入る
</main>
<!-- ▲ main -->

<!-- ▼ footer -->
<footer class="footer">
  <p><small>&copy; 2024 ハンバーガーメニュー</small></p>
</footer>
<!-- ▲ footer -->

</body>
</html>

style.css

  • メニュー以外の部分を押したときの設定も記述
@charset "UTF-8";

/* ----------------------------------------
  reset
---------------------------------------- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  min-height: 0vw;
}
ul {
  list-style: none;
}
a {
  color: inherit;
  text-decoration: none;
}
img {
  max-width: 100%;
  vertical-align: bottom;
}
html {
  font-size: 100%;
}

@media screen and (max-width: 767px) {
  html {
    font-size: 90%;
  }
}

/* ----------------------------------------
  body
---------------------------------------- */
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  background-color: #fff;
  color: #333;
  font-size: 1.0rem;
  font-family: "Noto Sans JP", sans-serif;
  line-height: 1.0;
}


/* ------------------------------------------
  header
------------------------------------------ */
#header {
  width: 100%;
  padding: 20px 0;
  background-color: #326ea2;
  text-align: center;
}
  #header > h1 {
    color: #fff;
    font-size: 1.5rem;
    text-align: center;
    line-height: 1.5;
    white-space: pre-line;
  }


/* ------------------------------------------
  footer
------------------------------------------ */
.footer {
  padding: 10px 0;
  background-color: #000;
  color: #fff;
  text-align: center;
}


/* ------------------------------------------
  main
------------------------------------------ */
.main {
  display: grid;
  place-content: center;
}


/* ------------------------------------------
  nav
------------------------------------------ */
.gnav {
  display: grid;
  place-content: center;
  position: fixed;
  top: 0;
  right: 0;
  max-width: 70vw;
  width: 100%;
  height: 100vh;
  padding: 20px;
  background-color: rgba(74, 74, 74, 0.8);
  opacity: 0;
  visibility: hidden;
  transform: translateX(100%);
  transition-property: all;
  transition-duration: .2s;
}
  .gnav a {
    display: block;
    padding-block: 20px;
    color: #fff;
    font-size: 24px;
  }


/* -------------- hamburger -------------- */
#hmb {
  position: fixed;
  top: 14px;
  right: 20px;
  width: 50px;
  height: 50px;
  padding: 0 8px;
  background-color: #326ea2;
  border: 1px solid #aaa;
  cursor: pointer;
  z-index: 1000;
}
  #hmb span {
    display: block;
    width: 32px;
    height: 3px;
    background-color: #fff;
    position: absolute;
    transition: .2s;
  }
    #hmb span:nth-of-type(1) {top: 22%;}
    #hmb span:nth-of-type(2) {top: 46%;}
    #hmb span:nth-of-type(3) {top: 72%;}

/* -------------- event -------------- */
.active #hmb {
  background-color: #000;
}
.active #hmb span:nth-of-type(1) {
  top: 48%;
  transform: rotate(45deg);
}
.active #hmb span:nth-of-type(2) {
  opacity: 0;
}
.active #hmb span:nth-of-type(3) {
  top: 48%;
  transform: rotate(-45deg);
}
.active .gnav {
  visibility: visible;
  opacity: 1;
  transform: translateX(0);
}

#mask {
  display: none;
  transition: all .5s;
}
.active #mask {
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: .1;
  z-index: 100;
  cursor: pointer;
}

script.js

$(function(){

  // ハンバーガーメニューをクリックしたとき
  $('#hmb').on('click', function() {
    if ($('#header').hasClass('active')) {
      $('#header').removeClass('active');
    } else {
      $('#header').addClass('active');
    }
  });

  // #maskのエリアをクリックした時にメニューを閉じる
  $('#mask').on('click', function() {
    $('#header').removeClass('active');
  });

  // リンクをクリックした時にメニューを閉じる
  $('.gnav a').on('click', function() {
    $('#header').removeClass('active');
  });

});