<header class="header">
<div class="header__layout">
<!-- ハンバーガーメニューのロゴ id="hamburger-logo" を追加 -->
<h1 id="hamburger-logo" class="header__logo">
<a href="#">ここにロゴ</a>
</h1>
<div id="hamburger-nav" class="header__nav">
<ul class="header__menus">
<li class="header__menu">
<a href="#">メニュー</a>
</li>
<li class="header__menu">
<a href="#">メニュー</a>
</li>
<li class="header__menu">
<a href="#">メニュー</a>
</li>
<li class="header__menu">
<a href="#">メニュー</a>
</li>
</ul>
</div>
<div id="hamburger" class="hamburger">
<div id="hamburger-icon" class="hamburger__icon">
<div class="hamburger__bar"></div>
<div class="hamburger__bar"></div>
<div class="hamburger__bar"></div>
</div>
<div id="hamburger-background" class="hamburger__background"></div>
</div>
</div>
</header>
/* リセットCSS */
a {
color: inherit;
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
/* ヘッダーで使用 */
.header {
position: fixed;
z-index: 10;
top: 0;
left: 0;
width: 100%;
height: 100px;
background-color: #5CC0EF;
}
.header__layout {
display: flex;
justify-content: space-between;
align-items: center;
height: inherit;
margin: 0 auto;
padding: 0 20px;
}
.header__logo {
position: relative;
z-index: 12;
}
.header__logo a {
font-size: 24px;
color: #fff;
}
.header__nav {
height: inherit;
}
@media screen and (max-width: 767px) {
.header__nav {
display: none;
}
}
.header__menus {
display: flex;
justify-content: center;
align-items: center;
gap: 40px;
height: inherit;
}
.header__menu {
height: inherit;
}
.header__menu a {
display: flex;
align-items: center;
justify-content: center;
height: inherit;
font-size: 18px;
color: #fff;
transition: all 0.3s;
}
.header__menu a:hover {
color: #ffe100;
}
/* ハンバーガーメニュー関連 */
.hamburger {
display: none;
}
@media screen and (max-width: 767px) {
.hamburger {
display: block;
}
}
.hamburger__icon {
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 40px;
height: 32px;
}
.hamburger__icon:hover .hamburger__bar {
background-color: #ffe100;
}
.hamburger__icon .hamburger__bar {
background-color: #fff;
border-radius: 2px;
width: 100%;
height: 3px;
transition: all 0.3s;
}
.hamburger__background {
display: none;
position: fixed;
z-index: 11;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: #5CC0EF;
}
/* ハンバーガーメニューのアニメーション */
/* ハンバーガーメニューのアイコンで使用 */
.js-hamburger .hamburger__icon {
position: relative;
z-index: 12;
}
.js-hamburger .hamburger__icon .hamburger__bar {
position: absolute;
}
.js-hamburger .hamburger__icon .hamburger__bar:nth-child(1) {
left: 0;
top: calc(50% - 1px);
transform: rotate(45deg);
}
.js-hamburger .hamburger__icon .hamburger__bar:nth-child(2) {
opacity: 0;
}
.js-hamburger .hamburger__icon .hamburger__bar:nth-child(3) {
bottom: calc(50% - 2px);
left: 0;
transform: rotate(-45deg);
}
/* ハンバーガーメニューのメニューで使用 */
.js-hamburger-nav {
display: block;
}
.js-hamburger-nav .header__menus {
position: fixed;
z-index: 12;
top: 100px;
left: 0;
width: 100%;
height: calc(100% - 100px);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.js-hamburger-nav .header__menu a {
height: auto;
}
.js-hamburger-nav .header__menu {
width: 100%;
height: auto;
}
/* ハンバーガーメニューを開いた時に背景を固定する */
.js-background-fixed {
height: 100%;
overflow: hidden;
width: 100%;
}
$(function(){
/* 変数の作成 */
const bodyElement = $("body");
const hamburger = $('#hamburger');
const hamburger_icon = $('#hamburger-icon');
const hamburger_nav = $('#hamburger-nav');
const hamburger_background = $('#hamburger-background');
const hamburger_logo = $('#hamburger-logo');
const hamburger_menu = $('#hamburger-nav a');
/* アイコンをクリックしたときの処理 */
hamburger_icon.on('click', function() {
hamburger.toggleClass('js-hamburger');
hamburger_nav.toggleClass('js-hamburger-nav');
hamburger_background.fadeToggle(300);
bodyElement.toggleClass('js-background-fixed');
});
/* ロゴをクリックしたときの処理 */
hamburger_logo.on('click', function() {
hamburger.removeClass('js-hamburger');
hamburger_nav.removeClass('js-hamburger-nav');
hamburger_background.fadeOut(300);
bodyElement.removeClass('js-background-fixed');
});
/* メニューをクリックしたときの処理 */
hamburger_menu.on('click', function() {
hamburger.removeClass('js-hamburger');
hamburger_nav.removeClass('js-hamburger-nav');
hamburger_background.fadeOut(300);
bodyElement.removeClass('js-background-fixed');
});
});
This Pen doesn't use any external CSS resources.