🐕
JS レスポンシブメニュー
作成日:
2023/10/26
0
レスポンシブメニューについて
今回はよく使われるメニューをレスポンシブ対応でいくつか紹介
<header>
<h1 class="logo">LOGO</h1>
<p class="btnNav">
<span></span>
<span></span>
<span></span>
</p>
<nav class="navWrap">
<ul class="navList">
<li><a href="#">ホーム</a></li>
<li><a href="#">会社概要</a></li>
<li><a href="#">事業紹介</a></li>
<li><a href="#">採用情報</a></li>
<li><a href="#">お問い合わせ</a></li>
</ul>
</nav>
</header>
<script>
// js
const btnNav = document.getElementsByClassName('btnNav')[0];
btnNav.addEventListener('click',function(){
const listTarget = btnNav.nextElementSibling;
if(listTarget.classList.contains('open') === false) {
listTarget.classList.add('open');
}else {
listTarget.classList.remove('open')
}
})
</script>
<style>
//css
/* メニュー */
header {
width: 100%;
display: flex;
justify-content: space-around;
align-items: center;
}
header .navWrap {
display: flex;
align-items: center;
}
header .btnNav {
display: none;/* 767px以下で表示 */
align-items: center;
flex-direction: column;
justify-content: center;
gap: 10px;
/* border: 1px solid black; */
width: 50px;
height: 50px;
cursor: pointer;
}
header p span {
width: 40px;
height: 3px;
background-color: black;
}
nav.navWrap ul.navList {
list-style: none;
display: flex;
justify-content: space-between;
gap: 1rem;
}
nav.navWrap ul.navList li a {
text-decoration: none;
color: black;
}
@media screen and (max-width:767px) {
header {
display: flex;
flex-wrap: wrap;
overflow-x: hidden;
}
header .logo,
p.btnNav {
width: 50%;
}
header nav.navWrap {
width: 100%;
height: 100vh;
}
header .btnNav {
display: flex;
}
header .navWrap {
display: block;
background-color: aquamarine;
transform: translateX(100%);
}
header .navWrap.open {
animation: open_sp 1s ease-in-out forwards;
}
@keyframes open_sp {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
nav.navWrap ul.navList {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
height: 100%;
}
}
</style>
jQuery
(参考サイト)
<div class="hamburger">
<div class="logo">LOGO</div>
<p class="btn-gNav">
<span></span>
<span></span>
<span></span>
</p>
<nav class="gNav">
<ul class="gNav-menu">
<li><a href="#">ホーム</a></li>
<li><a href="#">会社概要</a></li>
<li><a href="#">事業紹介</a></li>
<li><a href="#">採用情報</a></li>
<li><a href="#">お問い合わせ</a></li>
</ul>
</nav>
</div>
<style>
.hamburger {
margin: 20px auto 0;
width: 100%;
max-width: 900px;
display: flex;
justify-content: space-between;
}
.logo {
font-size: 40px;
font-weight: 700;
}
.hamburger .btn-gNav {
position: fixed;
top: 20px;
right: 20px;
width: 30px;
height: 24px;
z-index: 12;
box-sizing: border-box;
cursor: pointer;
-webkit-transition: all 400ms;
transition: all 400ms;
}
.hamburger .btn-gNav span {
position: absolute;
width: 100%;
height: 4px;
background: #ffffff;
border-radius: 10px;
-webkit-transition: all 400ms;
transition: all 400ms;
}
.hamburger .btn-gNav span:nth-child(1) {
top: 0;
}
.hamburger .btn-gNav span:nth-child(2) {
top: 10px;
}
.hamburger .btn-gNav span:nth-child(3) {
top: 20px;
}
.hamburger .btn-gNav.open span:nth-child(1) {
background: #ffffff;
top: 6px;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
transform: rotate(-45deg);
}
.hamburger .btn-gNav.open span:nth-child(2),
.hamburger .btn-gNav.open span:nth-child(3) {
top: 6px;
background: #ffffff;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
}
.btn-gNav {
display: none;
}
.gNav .gNav-menu {
display: flex;
justify-content: space-between;
}
.gNav .gNav-menu li {
margin: 0 auto;
padding: 0 5px;
}
@media screen and (max-width: 600px) {
.btn-gNav {
display: block;
}
.gNav {
position: fixed;
top: 0;
right: -100%;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
font-size: 16px;
box-sizing: border-box;
z-index: 1;
padding-top: 50px;
transition: .3s;
}
.gNav.open {
right: 0;
}
.gNav .gNav-menu {
padding: 0;
width: 100%;
height: 100%;
display: block;
flex-direction: column;
text-align: center;
/*justify-content: center;*/
}
.gNav .gNav-menu li {
width: 86%;
padding: 15px;
border-bottom: #525252 1px solid;
}
</style>
<script>
$(function () {
$('.btn-gNav').on("click", function () {
$('.gNav').toggleClass('open'); // メニューにopenクラスをつけ外しする
});
});</script>
input だけ
<header class="header">
<div class="navtext-container">
<div class="navtext">company</div>
</div>
<input type="checkbox" class="menu-btn" id="menu-btn">
<label for="menu-btn" class="menu-icon"><span class="navicon"></span></label>
<ul class="menu">
<li class="top"><a href="#home">home</a></li>
<li><a href="#skills">skills</a></li>
<li><a href="#projects">projects</a></li>
<li><a href="#contact">contact</a></li>
</ul>
</header>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
margin: 0;
font-family: "Futura", Helvetica, sans-serif;
}
/* Navbar & Navmenu color */
:root {
--background-navbar: rgba(55, 55, 55, 0.98);
}
.header {
background: var(--background-navbar);
position: fixed;
width: 100%;
height: 52px;
}
/* Nav items */
.menu {
list-style: none;
position: absolute;
width: 100%;
height: auto;
top: 0;
margin-top: 52px;
padding: 0 0 10px 0;
clear: both;
background: var(--background-navbar);
transition: 0.3192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.1008s;
transform: scale(1, 0);
transform-origin: top;
}
/* Hamburger menu button */
.menu-btn:checked ~ .menu {
transform: scale(1, 1);
transform-origin: top;
transition: 0.3192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.1008s;
}
/* Hamburger menbu text */
.menu a {
text-decoration: none;
font-weight: 500;
letter-spacing: 2px;
font-size: 16px;
text-transform: capitalize;
color: #ddd;
opacity: 0;
transition: 0.5s;
}
.menu li {
border-top: 1px solid rgb(75, 75, 75);
padding: 15px 0;
margin: 0 54px;
opacity: 0;
transition: 0.5s;
}
.menu-btn:checked ~ .menu a,
.menu-btn:checked ~ .menu li {
opacity: 1;
transition: 0.3192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.2s;
}
.menu-btn {
display: none;
}
.menu-icon {
display: inline-block;
position: relative;
cursor: pointer;
padding: 24px 14px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.navicon {
background: #ddd;
display: block;
height: 3px;
width: 26px;
position: relative;
transition: 0.3192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.1008s;
}
.navicon:before,
.navicon:after {
content: "";
display: block;
height: 100%;
width: 100%;
position: absolute;
background: #ddd;
transition: 0.3192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.1008s;
}
.navicon:before {
top: 9px;
}
.navicon:after {
bottom: 9px;
}
/* Hamburger Menu Animation Start */
.menu-btn:checked ~ .menu-icon .navicon:before {
transform: rotate(-45deg);
}
.menu-btn:checked ~ .menu-icon .navicon:after {
transform: rotate(45deg);
}
.menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before {
top: 0;
}
.menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
bottom: 0;
}
.menu-btn:checked ~ .menu-icon .navicon {
background: rgba(0, 0, 0, 0);
transition: 0.2192s cubic-bezier(0.04, 0.04, 0.12, 0.96) 0.1008s;
}
/* Hamburger Menu Animation End */
/* Navbar Container */
.navtext-container {
width: 100%;
height: 52px;
position: absolute;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
}
/* Navbar Text */
.navtext {
position: absolute;
text-transform: uppercase;
color: #ddd;
letter-spacing: 4px;
font-size: 20px;
}
</style>
感想
正直どれでもいいけど、inputの方がなんか良い気がする
フロントエンドエンジニアを頑張って勉強中。
最近はVueやNuxtを勉強中です。
コミュニティなどあれば参加してみたいと思ってます。
どうぞ、生暖かい目で見ていただけると幸いです。