1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ハンバーガーメニューの作成

Posted at

スクリーンショット 2020-04-02 18.30.51.png
スクリーンショット 2020-04-02 18.30.58.png

HTML5


<body>
    <!-- ハンバーガーボタン -->
    <div class="ham" id="ham">
    <!-- ハンバーガーボタンのライン -->
        <span class="ham_line ham_line1"></span>
        <span class="ham_line ham_line2"></span>
        <span class="ham_line ham_line3"></span>
    </div>
    <!-- メニューの中身 -->
    <div class="menu_wrapper" id="menu_wrapper">
        <div class="menu">
            <ul>
                <li><a href="#1" style="color: #FFF;">メニュー1</a></li>
                <li><a href="#2" style="color: #FFF;">メニュー2</a></li>
                <li><a href="#3" style="color: #FFF;">メニュー3</a></li>
            </ul>
        </div>
    </div>

CSS


.ham{
    position: relative;
    width: 40px;
    height: 40px;
    cursor: pointer;
    background-color: rgba(0, 0, 255, 0.911);
}
/* ハンバーガボタンのライン */
.ham_line {
    transition: all 0.3s;
	position: absolute;
	left: 10px;
	width: 20px;
	height: 2px;
    background-color: #FFF;
}
.ham_line1 {
    top: 10px;
}
.ham_line2 {
	top: 18px;
}
.ham_line3 {
	top: 26px;
}

/* Javascriptでクリックイベントが発火した後の処理 ハンバーガボタンのライン */
.clicked .ham_line1{
    transform: rotate(45deg);
    top: 20px;
}

.clicked .ham_line2{
    width: 0px;
}

.clicked .ham_line3{
    transform: rotate(-45deg);
    top: 20px;
}
/* メニューの表示 */
.menu {
    position: fixed;
    left: -400px;
    width: 300px;
	height: 300px;
    background-color: rgba(0, 0, 255, 0.849);
    transition: all 0.5s;
    color: #FFF;
}

.clicked .menu {
	left: 8px;
}

JavaScript


  const ham = document.querySelector('#ham');
  const menu_wrapper = document.querySelector('#menu_wrapper')

// addEventListener さまざまなイベント処理を実行することができるメソッド
  ham.addEventListener('click', function(){ 
// classListとは、対象要素に設定しているクラスを配列のように扱えるオブジェクト
    ham.classList.toggle('clicked');
    menu_wrapper.classList.toggle('clicked')
  });

addEventListener

https://www.sejuku.net/blog/57625

classListとは

https://techacademy.jp/magazine/22308

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?