LoginSignup
1
0

More than 3 years have passed since last update.

【Javascript】ヘッダーの ≡ (ハンバーガーアイコン)を押下すると、メニューがしゅっと出てくるやつをJavaScriptでやってみた

Last updated at Posted at 2021-03-04

自己備忘録用のまとめです。

レスポンシブデザインに欠かせないメニューアイコンとメニューモーダルの表現。
jQuery離れが散見されることもあり、jQueryに頼らずJavaScriptでやってみた。

CSSを上手く使うととそんなに難しくない。(はず)
 
使う言語  HTML5、 CSS3、 JavaScript
 
 
まずはHTML。とてもシンプル。

index.html
<header>
  <div class="header-inner">
    <div class="header-title">
      <h1>ヘッダータイトル</h1>
    </div>
    <!-- メニュー開閉アイコン -->
    <div id="humberger-icon" class="open-menu"></div>
  </div>
</header>
<!-- メニューモーダル -->
<div id="header-mordal">
  <nav>
    <ul id="header-mordal-list">
      <li><a class="header-menu-btn" href="#">menu 1</a></li>
      <li><a class="header-menu-btn" href="#">menu 2</a></li>
      <li><a class="header-menu-btn" href="#">menu 3</a></li>
    </ul>
  </nav>
</div>

 
続いて、CSS。ちょっぴり長め。

ポイント
今回メニューモーダルは右から左にスライドしながら表示させたいので
・メニューモーダルを right: -100%; で画面外表示にして初期表示は隠す。
・transition プロパティを使ってアニメーション時間をcssにて設定。
・開くボタン/閉じるボタン それぞれのcssを書く
 htmlには開くボタンのclass名だけ書いておく

※長くなるのであえてここには書いてませんが、
 はじめにリセットCSSを記述してください

master.css
header {
  padding: 15px;
  width: 100%;
  height: auto;
  backgroud-color: #f8f7f5;
  position: fixed;
  z-index: 10000;
}

.header-inner {
  width: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  align-items: center;
  height: auto;
}

.header-title a h1 {
  padding: 0;
  color: #444;
  font-size: 22px;
}

/* メニューモーダルのスタイル */
#header-mordal {
  z-index: 9999;
  width: 100%;
  height: auto;
  background-color: #f8f7f5;
  padding: 80px 30px 50px 30px;
  position: fixed;
  text-align: center;
  right: -100%;     /* 右側へ画面外表示して初期表示ではメニューを隠す */
  transition: .5s; /* アニメーション時間 */
  transition-timing-function: ease; /* アニメーションの速さ具合的な */
}

#header-mordal-list li {
  font-size: 1em;
  font-weight: normal;
  padding: 30px 0;
  color: #7a7a7a;
}

#header-mordal-list li a {
  text-decoration: none;
  padding-bottom: 8px;
}

/* メニューを開くためのボタンのスタイル(三本線アイコン) */
.open-menu {
   position: relative;
   display: inline-block;
}

.open-menu:before,
.open-menu:after {
   content: '';
   position: absolute;
}

.open-menu:before {bottom: 9px;}
.open-menu:after {top: 9px;}

.open-menu,
.open-menu:before,
.open-menu:after {
   display: block;
   z-index: 10001;
   height: 2px;
   width: 30px;
   border-radius: 2px;
   background-color: #000;
   transition: .3s;
}

/* ここから閉じるボタンのスタイル(✕アイコン) */
.close-menu {
   position: relative;
   display: inline-block;
}

.close-menu:before,
.close-menu:after {
   content: '';
   position: absolute;
}

.close-menu,
.close-menu:before,
.close-menu:after {
   display: block;
   z-index: 10001;
   height: 2px;
   width: 30px;
   border-radius: 2px;
   background-color: #000;
   transition: .3s;
}

.close-menu:before {
   bottom: 0px;
   transform: rotate(-45deg);
}

.close-menu:after {
   top: 0px;
   transform: rotate(45deg);
}

.close-menu {background-color: transparent;}

 
ここから本番。JSの登場。超要約すると、
・メニューボタンを押下したか否かを判定して、
 メニューモーダルのスタイルを画面内・画面外に変更する。
・toggleを使って、メニューの開閉ボタンを交互に切り替える。

master.js
var isOpen = false;
var menu = document.getElementById('header-mordal');
document.getElementById('humberger-icon').addEventListener('click', function() {

    if (isOpen) {
        isOpen = false;
        menu.style.right = "-100%";  //メニュー非表示
    } else {
        isOpen = true;
        menu.style.right = "0";  //メニュー表示
    }

    this.classList.toggle('close-menu');
}, false);

 
メニューを上から下にスライド表示させたい場合は、right としているところを top に書き換える。
cssも top: -100%; にするのを忘れなきよう。

開くボタン↔閉じるボタンに変わる時のアニメーションをCSSに頼ることで、
JSは超シンプルな内容で済みました。

ちなみに、inputタグを使えばJSにすら頼らずともCSSだけで完結も可能ですが、
私はJSありきの方がやりやすい。
 
 
おしまい。どろん。

1
0
0

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
1
0