レスポンシブ対応グローバルナビ(上部固定付き)

ナビはヘッダ内のピンクの部分。(ページの上部に固定する部分付きver.)


<div id="globalNavi">
	<a id="menu"><span class="material-symbols-outlined">menu</span></a>
	<nav>
		<ul>
			<li><a href="/">INDEX</a></li>
			<li><a href="#">CONTENTS 1</a></li>
			<li><a href="#">CONTENTS 2</a></li>
		</ul>
	</nav>
</div>

/*====================================================
*	Navigation
====================================================*/
@media screen and (min-width: 1024px) {
	#globalNavi a#menu {
		display:none;
		}
	#globalNavi nav {
		display: block !important;
		}
	}
#globalNavi {
	width: 100%;
	margin: 0 auto;
	padding: .8rem 0;
	background: rgba(26,25,30,.7);
	overflow:visible;
 	}
#globalNavi ul {
	list-style-type: none;
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-end;	
	align-content: center;
	}
#globalNavi nav a {
	display: block;
	margin-right: .3rem;
	padding: .5rem 2rem;
	white-space: nowrap;
	text-align: center;
	text-decoration: none;
	color: #fff;
	background: #d02090;
	}
#globalNavi nav a:hover {
	background-color:#f3c1e1;
	text-decoration: none;
	}

/*[ Media Query ] --------------------------- */
@media screen and (max-width: 1023.9px) {
	#globalNavi {
		padding: 0;
		}
	}

/* [Toggle Menu ]
------------------------------------------------ */
@media screen and (max-width: 1023.9px) {
	#globalNavi a#menu {
		display: block;
		text-align: right;
		margin:0;
		padding: 1rem 2rem 0 0;
		color:#fff;
		background-color:#d02090;
		}
	#globalNavi a#menu span {
		font-size: 4rem;
		line-height: ;
		letter-spacing: .2rem;
		}
	#globalNavi a#menu:hover {
		color: #ff0;
		text-decoration: none;
		background-color:#f698d4;
		color:#d02090;
		}
	#globalNavi ul {
		display: block;
		justify-content: center;
		}
	#globalNavi nav a {
		padding: 1.5rem 2rem;
		color:#d02090;
		background-color:#fff;
		border-bottom:1px #134192 dashed;
		}
		#globalNavi ul li:first-child a {
			border-top:1px #fff dashed;
			}
	}

/*Kotei navi
------------------------------------------------ */
#globalNavi.fixed {
	min-height: 6rem;
	margin: 0 auto;
	padding-bottom:0;
	background: rgba(26,25,30,.7);
	position: fixed !important;
	z-index: 500 !important;
	top: 0;
	left: 0;
	}
#globalNavi.fixed nav ul {
	margin:0 auto;
	display: flex;
	flex-wrap: wrap;
	align-items: flex-end;
	list-style: none;
	}

#globalNavi.fixed {
	overflow:visible;
	}
/*[ Media Query ] --------------------------- */
@media screen and (max-width: 1023.9px) {
	#globalNavi.fixed nav ul {
		display: block !important;
		}
	}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
//固定ナビゲーション
$(function() {
	var nav = $('#globalNavi');
	//表示位置
	var navTop = nav.offset().top+100;
	//ナビゲーションの高さ(シャドウ付きならその分だけ足す)
	var navHeight = nav.height()+0;
	var showFlug = false;
	nav.css('top', -navHeight+'px');
	$(window).scroll(function () {
		var winTop = $(this).scrollTop();
		if (winTop >= navTop) {
			if (showFlug == false) {
				showFlug = true;
				nav
					.addClass('fixed')
					.stop().animate({'top' : '0px'}, 300);
			}
		} else if (winTop <= navTop) {
			if (showFlug) {
				showFlug = false;
				nav.stop().animate({'top' : -navHeight+'px'}, 200, function(){
					nav.removeClass('fixed');
				});
			}
		}
	});
});

//スマホ用ナビゲーションスライドダウン
$(function () {
	$("#globalNavi nav").hide();
	$("#menu").click(function () {
		$(this).toggleClass("menuOpen").next().slideToggle();
	});
})

TOP