ブログ

【CSS】レスポンシブ対応のハンバーガーメニューサンプル

レスポンシブ対応のサイト制作時において、PCサイト用のグローバルメニューは、スマホ時などにはコンパクトにするために、ハンバーガーメニューなどにするケースがあります。ちなみに、なぜハンバーガーメニューと呼ばれるかというと、メニューを展開するアイコンの三本線が、ハンバーガーに見えることからそう呼ばれている様です。

ハンバーガーメニューは、CSSだけで実装できるものやJQueryと併用するものなど様々です。

CSSのみで実装(ドロワーメニュー)

<div id="drawer">
	<input id="input" type="checkbox" class="unshown">
	<label id="open" for="input"><span></span></label>
	<label class="unshown" id="close" for="input"></label>
	<div id="menu-content">メニューの中身</div>
</div>
#drawer {
  position: relative;
}
.unshown {
  display:none;
}
#open {
  display: inline-block;
  width: 30px;
  height: 22px;
  vertical-align: middle;
}
#open span, #open span::before, #open span::after {
  position: absolute;
  height: 3px;
  width: 25px;
  border-radius: 3px;
  background: #555;
  display: block;
  content: "";
  cursor: pointer;
}
#open span::before {
  bottom: -8px;
}
#open span::after {
  bottom: -16px;
}
#close {
  display: none;
  position: fixed;
  z-index: 99;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: black;
  opacity: 0;
  transition: .3s ease-in-out;
}
#menu-content {
  overflow: auto;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  width: 90%;
  max-width: 330px;
  height: 100%;
  background: #fff;
  transition: .3s ease-in-out;
  transform: translateX(-105%);
}
#input:checked ~ #close {
  display: block;
  opacity: .5;
}
#input:checked ~ #menu-content {
  transform: translateX(0%);
  box-shadow: 6px 0 25px rgba(0,0,0,.15);
}

デモを見る

JQuery(JavaScript)を使ったメニュー

<header>
<h1>サンプルサイト</h1>
  <nav class="gnavi__pc-style">
    <ul>
      <li><a href="#">メニュー1</a></li>
      <li><a href="#">メニュー2</a></li>
      <li><a href="#">メニュー3</a></li>
      <li><a href="#">メニュー4</a></li>
    </ul>
  </nav>

<!--768px以下で表示-->
  <div class="menu-btn">
    <span></span>
    <span></span>
    <span></span>
  </div>
  <nav class="gnavi__sp-style">
    <ul>
      <li><a href="#">メニュー1</a></li>
      <li><a href="#">メニュー2</a></li>
      <li><a href="#">メニュー3</a></li>
      <li><a href="#">メニュー4</a></li>
    </ul>
  </nav>
<!--/768px以下で表示-->
</header>
header {
  width: 100%;
  background-color: #ddd;
  padding: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative; 
}
h1 {
  font-size: 20px;
  font-weight: bold;
}
.gnavi__pc-style {
  margin: 0 0 0 auto;
}
.gnavi__pc-style ul {
  display: flex;
  justify-content: space-between;
}
.gnavi__pc-style ul li:not(:last-of-type) {
  margin-right: 10px;
}

/*ここから下がハンバーガーメニューのスタイル*/
.gnavi__sp-style {
  display: none;
}
@media screen and (max-width: 768px) {
.gnavi__pc-style {
  display: none;    
}
.gnavi__sp-style {
  display: block;
}

/*メニューボタン 展開前*/
.menu-btn {
  display : block;
  position: fixed;
  z-index: 3;
  right: 30px;
  top: 20px;
  width: 42px;
  height: 42px;
  cursor: pointer;
  text-align: center;
}
.menu-btn span {
  display : block;
  position: absolute;
  width: 30px;
  height: 2px ;
  left: 6px;
  background: #555;
  transition: 0.3s ease-in-out;
}
.menu-btn span:nth-of-type(1) {
  top: 10px;
}
.menu-btn span:nth-of-type(2) {
  top: 20px;
}
.menu-btn span:nth-of-type(3) {
  top: 30px;
}
  
/*メニューボタン 展開後*/
.menu-btn.active span:nth-child(1) {
  top: 16px;
  left: 6px;
  background :#fff;
  transform: rotate(-45deg);
}
.menu-btn.active span:nth-child(2),
.menu-btn.active span:nth-child(3) {
  top: 16px;
  background :#fff;
  transform: rotate(45deg);
}

/*スマホ用のメニュー*/
.gnavi__sp-style {
  position: fixed;
  z-index: 2;
  top: 0;
  left: 0;
  color: #fff;
  background-color: rgba(0,0,0,0.7);
  text-align: center;
  width: 100%;
	height: 100vh;
  opacity: 0;
  transition: opacity .6s ease, visibility .6s ease;
	display: flex;
	align-items: center;
}
.gnavi__sp-style ul {
  margin: 0 auto;
  padding: 0;
  width: 100%;
}
.gnavi__sp-style ul li {
  list-style-type: none;
  padding: 0;
  width: 100%;
  transition: .4s all;
}
.gnavi__sp-style ul li:last-of-type {
  padding-bottom: 0;
}
.gnavi__sp-style ul li:hover{
  background :#ddd;
}
.gnavi__sp-style ul li a {
  display: block;
  color: #fff;
  padding: 1em 0;
  text-decoration :none;
}
.gnavi__sp-style.active {
  opacity: 1;
}
}
$(function() {
    $('.menu-btn').click(function() {
        $(this).toggleClass('active');
 
        if ($(this).hasClass('active')) {
            $('.gnavi__sp-style').addClass('active');
        } else {
            $('.gnavi__sp-style').removeClass('active');
        }
    });
});

デモを見る

この記事を書いた人管理人

SAKURAGRAPHICA代表
会社員の傍ら、フリーランスでWEBサイトの制作やWordPressによるCMSの構築・障がい者の就労支援としてホームページ制作の職業指導員も行っております。

SAKURAGRAPHICAができること

最近の記事 おすすめ記事
PAGE TOP