0
0

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 1 year has passed since last update.

HTML CSS JavaScript 基礎 ハンバーガーメニュー の作り方

Last updated at Posted at 2022-09-23

初心者エンジニアなりにハンバーガーメニューの作り方をメモ用に書きました

全体コード

index.html
<header>
    <a id="menu_button">
        <div></div>
        <div></div>
        <div></div>
    </a>
    <nav id="nav">
        <ul>
            <li>menu1</li>
            <li>menu2</li>
            <li>menu3</li>
            <li>menu4</li>
        </ul>
    </nav>
</header>
style.css
@charset "utf-8";
*, *::before, *::after{
    box-sizing: border-box;
}
a{
    text-decoration: none;
}
ul{
    list-style: none;
}

#menu_button{
    display: block;
    height: 50px;
    width: 50px;
    background: rgb(117, 117, 117);
    position: fixed;
    top: 0;
    left: 0;
}
#menu_button div{
    height: 1px;
    width: 60%;
    background-color: #fff;
    position: absolute;
    top: 50%;
    right: 50%;
    transform: translateX(50%);
    transition: ease .4s;
}
#menu_button div:nth-of-type(1){
    transform: translate(50%,-10px);
}
#menu_button div:nth-of-type(3){
    transform: translate(50%,10px);
}

#nav{
    width: 30%;
    height: 100vh;
    position: fixed;
    top: 0;
    left: -30%;
    background-color: gray;
    font-size: 16px;
    z-index: -1;
    padding-top: 50px;
    transition: ease .4s;
}
#nav.active{
    transform: translateX(100%);
}

#menu_button.active {
    transform: translateX(0);
  }
#menu_button.active div:nth-of-type(1){
    transform: rotate(45deg);
    top: 25px;
    left: 10px;
  
} 
#menu_button.active div:nth-of-type(2) {
    opacity: 0;
}
#menu_button.active div:nth-of-type(3) {
    transform: rotate(-45deg);
    bottom: 25px;
    left: 10px;
}

script.js
const menuButton = document.getElementById("menu_button")
const nav = document.getElementById("nav")

// menu_buttonをとってくる→クリックされたとき
menuButton.addEventListener("click",() =>{
    menuButton.classList.toggle("active")
    nav.classList.toggle("active")
})

メニューボタンを作る

<a id="menu_button">
    <div></div>
    <div></div>
    <div></div>
</a>
#menu_button{
    display: block;
    height: 50px;
    width: 50px;
    background: rgb(117, 117, 117);
    position: fixed;
    top: 0;
    left: 0;
}
#menu_button div{
    height: 1px;
    width: 60%;
    background-color: #fff;
    position: absolute;
    top: 50%;
    right: 50%;
    transform: translateX(50%);
    transition: ease .4s;
}
#menu_button div:nth-of-type(1){
    transform: translate(50%,-10px);
}
#menu_button div:nth-of-type(3){
    transform: translate(50%,10px);
}

div要素を3つ作り、width60%、height1pxの要素を作ります

div:nth-of-type(1)で1つ目のdivと
div:nth-of-type(3)3つ目のdivを上下にtransformでずらします

jsでtoggleを使いクリックで切り替え機能を追加する

const menuButton = document.getElementById("menu_button")
const nav = document.getElementById("nav")

//クリックされたとき
menuButton.addEventListener("click",() =>{
    menuButton.classList.toggle("active")
    nav.classList.toggle("active")
})

メニューボタンと表示、非表示させるnavをdocument.getElementByIdを使い取得します

menu_buttonがクリックされたときにclassList.toggleを使いmenu_buttonとnavをactiveにします

activeになった時にメニューボタンを×マークにし、navを表示させます

#menu_button.active {
    transform: translateX(0);
  }
#menu_button.active div:nth-of-type(1){
    transform: rotate(45deg);
    top: 25px;
    left: 10px;
  
} 
#menu_button.active div:nth-of-type(2) {
    opacity: 0;
}
#menu_button.active div:nth-of-type(3) {
    transform: rotate(-45deg);
    bottom: 25px;
    left: 10px;
}

まず1つ目、2つ目のdiv要素をtransform: rotate(45deg);で回転させます
そして回転される支点は左上になってしまうのでtopとleftを使い位置を調整します

#nav{
    width: 30%;
    height: 100vh;
    position: fixed;
    top: 0;
    left: -30%;
    background-color: gray;
    font-size: 16px;
    z-index: -1;
    padding-top: 50px;
    transition: ease .4s;
}
#nav.active{
    transform: translateX(100%);
}

navは元の位置を画面外にしておきactiveになった時にtransformで持ってきています

image.png
  ↓
image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?