Humburger Navigation Smaple01
2023年3月17日
基本的なハンバーガーナビのコード。
mobile基準のレスポンシブ仕様。
モバイル表示の際にメニュー下部にSNS(Instagram)アイコン表示させている。
(Kureai BAKERYのsammpleより)
HTMLは以下
<div id="nav-btn"> <div class="nav_toggle"> <span></span> <span></span> <span></span> </div> </div> <nav id="global_nav"> <ul class="nav_list"> <li class="nav_list-item"><a href="">CONCEPT</a></li> <li class="nav_list-item"><a href="">LINE UP</a></li> <li class="nav_list-item"><a href="">TOPICS</a></li> <li class="nav_list-item"><a href="#access">ACCESS</a></li> <li class="nav_list-item"><a href="">CONTACT</a></li> </ul> <div class="header_sns"><a href=""><img src="./images/insta_circle_bk.svg"></a></div> </nav>
SCSS以下
scssなので_mixin.scssでmedia query設定後読込み
/* navigation */
#global_nav {
box-sizing: border-box;
position: fixed;
top: 0;
right: 0;
width: 100%;
height: 100%;
font-family: 'Roboto Slab', serif;
display: block;
transform: translate(100%);
transition: .5s;
background-color: rgba(44, 44, 44, 0.786);
z-index: 999;
@include m.mq(tab) {
padding: 70px 0 10px 0;
position: relative;
width: 700px;
height: 120px;
top: auto;
right: auto;
display: flex;
transform: none;
transition: none;
background-color: #FFF;
}
}
#global_nav.open {
transform: translateZ(0);
}
.nav_list{
padding-top: 120px;
width: 100%;
&-item {
padding: 15px 30px;
display: block;
font-size: 110%;
text-align: center;
a {
position: relative;
color: v.$color-wh;
&:hover {
font-size: 90%;
}
}
}
@include m.mq(tab) {
padding-top: 0;
height: 45px;
position: relative;
display: flex;
justify-content:space-between;
flex-wrap: wrap;
&-item {
padding: 10px 10px;
font-size: 90%;
a {
display: block;
font-size: 90%;
color: v.$color-text;
&::after {
position: absolute;
left: 0;
content: '';
width: 100%;
height: 2px;
background: v.$color-bk;
bottom: -7px;
transform: scale(0, 1);
transform-origin: left top;
transition: transform 0.3s;
}
&:hover {
color: v.$color-text;
&::after {
transform: scale(1, 1);
}
}
}
}
}
}
#nav-btn {
top: 15px;
right: 20px;
transition: .03s;
position: fixed;
z-index: 9999;
cursor: pointer;
width: 50px;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
@include m.mq(tab) {
display: none;
}
}
#nav-btn.active .nav_toggle {
span {
&:nth-of-type(1) {
transform: translateY(7px) rotate(-45deg);
background-color: #fff;
}
&:nth-of-type(2) {
opacity: 0;
}
&:nth-of-type(3) {
transform: translateY(-7px) rotate(45deg);
background-color: #fff;
}
}
}
.nav_toggle {
width: 40px;
height: 16px;
position: relative;
span {
display: inline-block;
position: absolute;
left: 0;
width: 100%;
height: 1px;
background-color: #363636;
transition: all 0.5s;
&:nth-of-type(1) {
top: 0;
}
&:nth-of-type(2) {
top: 7px;
}
&:nth-of-type(3) {
bottom: 0;
}
}
}
javascript jQuery利用版
ページ内リンクがあった場合でもnavが消えるようにしている。
jQuery(function() {
//navigation
jQuery(window).on('load', function() {
jQuery('#nav-btn').on('click', function () {
if (jQuery(this).hasClass('active')) {
jQuery(this).removeClass('active');
jQuery('#global_nav').removeClass('open');
} else {
jQuery(this).addClass('active');
jQuery('#global_nav').addClass('open');
}
//以下、メニュー部分をクリックしてもglobal_navは消える
jQuery('#global_nav a').on('click', function() {
jQuery('#global_nav').removeClass('open');
jQuery("#nav-btn").removeClass('active');
})
});
jQueryを利用しない版
window.onload = function() {
const nav = document.getElementById('global_nav');
const btn = document.getElementById('nav-btn');
btn.addEventListener('click', function() {
nav.classList.toggle('open');
btn.classList.toggle('active');
});
nav.addEventListener('click', function() {
nav.classList.remove('open');
btn.classList.remove('active');
})
};