レスポンシブヘッダーメニューを考えた

1個目

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="master.css">
    <script src="main.js" charset="utf-8"></script>
    <title>ヘッダーサンプル</title>
  </head>
  <body>

    <nav id="sk-nav">
      <ul id="sk-menu">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">Help</a></li>
      </ul>
    </nav>

    <div id="nav-icon">
        ■
    </div>



<script type="text/javascript">


      const icon = document.getElementById('nav-icon')
      const skMenu = document.getElementById('sk-nav')

      icon.addEventListener('click',() => {

        if(skMenu.classList.contains("active")){
          
            skMenu.classList.remove("active")

        }else if(!skMenu.classList.contains("active")){
            skMenu.classList.add("active")
        }

      })


</script>


  </body>
</html>

body{
  background:gray;
  margin:0;
  padding:0;
}
nav#sk-nav{
  display: flex;
  flex-direction: column;
  justify-content: center;
  background:#000;
  height:55px;

  position: relative;
  transition: .4s;
}
nav#sk-nav ul{
  position: relative;
  top:0;
  left:0;
  margin:0;
  padding:0;
  list-style: none;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  transition: .3s;
}


nav#sk-nav ul li a{
  color:#fff;
  display: block;
  text-decoration: none;
  transition: .3s;
}

nav#sk-nav ul li a:hover{
  color:gold;
}

#nav-icon{
  color:#fff;
  font-size:2em;
  cursor: pointer;
  position: absolute;
  top:10px;
  left:20px;
  display: none;
  opacity: 0;
  transition: .5s;
}


@media(max-width:640px){

  nav#sk-nav{
    height:0;
    top:-150px;
  }
  nav#sk-nav.active{
    height:100%;
    top:0px;
  }


  nav#sk-nav ul{
    flex-direction: column;
    align-items: center;
    height:100%;
  }
  nav#sk-nav ul li{
    padding:10px 0;
    width:100%;
    text-align:center;
    transition: .3s;
  }
  nav#sk-nav ul li:hover{
    background:rgba(90,90,90,1);
  }
  nav#sk-nav ul li a{
    display: block;
    width:100%;
  }

  #nav-icon{
    display: block;
    opacity: 1;
  }
}

2個目

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="master.css">
    <script src="main.js" charset="utf-8"></script>
    <title>ヘッダーサンプル</title>
  </head>
  <body>

    
    <div id="sk-mask">
      <nav id="sk-mask-nav">
        <ul id="sk-mask-menu">
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li><a href="#">Help</a></li>
        </ul>
      </nav>
    </div>


      <nav id="sk-nav">
        <ul id="sk-menu">
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li><a href="#">Help</a></li>
        </ul>
      </nav>

      <div id="nav-icon">
          ■
      </div>





<script type="text/javascript">


      const icon = document.getElementById('nav-icon')
      const skMenu = document.getElementById('sk-nav')
      const mask = document.getElementById('sk-mask')

      icon.addEventListener('click',() => {

        if(mask.classList.contains("active")){

          mask.classList.remove("active");

        }else if(!mask.classList.contains("active")){

            mask.classList.add("active")

        }

      })


</script>


  </body>
</html>

body{
  background:gray;
  margin:0;
  padding:0;
}

nav#sk-nav{
  display: flex;
  flex-direction: column;
  justify-content: center;
  background:#000;
  height:55px;

  position: relative;
  transition: .4s;
}
nav#sk-nav ul{
  position: relative;
  top:0;
  left:0;
  margin:0;
  padding:0;
  list-style: none;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  transition: .3s;
}


nav#sk-nav ul li a{
  color:#fff;
  display: block;
  text-decoration: none;
  transition: .3s;
}

nav#sk-nav ul li a:hover{
  color:gold;
}

#nav-icon{
  color:#fff;
  font-size:2em;
  cursor: pointer;
  position: absolute;
  top:10px;
  left:20px;
  display: none;
  opacity: 0;
  transition: .5s;
}



#sk-mask{
  background:rgba(0,0,0,0.9);
  width:100%;
  position: absolute;
  top:0;
  left: 0;
  height:0;
  opacity: 0;
  transition: .4s;
}

#sk-mask.active{
  height:100vh;
  opacity: 1;
}

#sk-mask-nav{
  width:100%;
  height:100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
#sk-mask-nav ul li{
  padding:20px 0;
}
#sk-mask-nav ul li a{
  text-decoration: none;
  color:#fff;
  transition: .4s;
}
#sk-mask-nav ul li a:hover{
  color:gold;
}






@media(max-width:640px){



  nav#sk-nav{
    top:-150px;
  }
  nav#sk-nav.active{
    height:100%;
    top:0px;
  }
  nav#sk-nav.resume{

  }

  nav#sk-nav ul{
    flex-direction: column;
    align-items: center;
    height:100%;
  }
  nav#sk-nav ul li{
    padding:10px 0;
    width:100%;
    text-align:center;
    transition: .3s;
  }
  nav#sk-nav ul li:hover{
    background:rgba(90,90,90,1);
  }
  nav#sk-nav ul li a{
    display: block;
    width:100%;
  }

  #nav-icon{
    display: block;
    opacity: 1;
  }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です