# 8.반응형 햄버거메뉴

# 1. 자바스크립트

미리보기

index.html

<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Responsive Navigation Bar</title>
		<!-- Font Awesome Icons -->
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />
		<!-- Google Fonts -->
		<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700&display=swap" rel="stylesheet" />
		<!-- Stylesheet -->
		<link rel="stylesheet" href="style.css" />
	</head>
	<body>
		<header>
			<nav>
				<a href="#home" id="logo">Site Logo</a>
				<i class="fas fa-bars" id="ham-menu"></i>
				<ul id="nav-bar">
					<li>
						<a href="#home">Home</a>
					</li>
					<li>
						<a href="#about">About</a>
					</li>
					<li>
						<a href="#services">Services</a>
					</li>
					<li>
						<a href="#team">Team</a>
					</li>
					<li>
						<a href="#contact">Contact</a>
					</li>
				</ul>
			</nav>
		</header>
		<section id="home">
			<h1>Home Section</h1>
		</section>
		<!-- Script -->
		<script src="script.js"></script>
	</body>
</html>

style.css

* {
	padding: 0;
	margin: 0;
	box-sizing: border-box;
}
html {
	font-size: 62.5%;
}
*:not(i) {
	font-family: 'Poppins', sans-serif;
}
header {
	position: fixed;
	width: 100%;
	background-color: #2c8eec;
	padding: 3rem 5rem;
}
nav {
	display: flex;
	justify-content: space-between;
	align-items: center;
}
nav ul {
	list-style: none;
	display: flex;
	gap: 2rem;
}
nav a {
	font-size: 1.8rem;
	text-decoration: none;
}
nav a#logo {
	color: #000000;
	font-weight: 700;
}
nav ul a {
	color: #ffffff;
	font-weight: 600;
}
nav ul a:hover {
	border-bottom: 2px solid #ffffff;
}
section#home {
	height: 100vh;
	display: grid;
	place-items: center;
}
h1 {
	font-size: 4rem;
}
#ham-menu {
	display: none;
}
nav ul.active {
	left: 0;
}
@media only screen and (max-width: 991px) {
	html {
		font-size: 56.25%;
	}
	header {
		padding: 2.2rem 5rem;
	}
}
@media only screen and (max-width: 767px) {
	html {
		font-size: 50%;
	}
	#ham-menu {
		display: block;
		color: #ffffff;
	}
	nav a#logo,
	#ham-menu {
		font-size: 3.2rem;
	}
	nav ul {
		background-color: black;
		position: fixed;
		left: -100vw;
		top: 73.6px;
		width: 100vw;
		height: calc(100vh - 73.6px);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-around;
		transition: 1s;
		gap: 0;
	}
}
@media only screen and (max-width: 575px) {
	html {
		font-size: 46.87%;
	}
	header {
		padding: 2rem 3rem;
	}
	nav ul {
		top: 65.18px;
		height: calc(100vh - 65.18px);
	}
}

script.js

let hamMenuIcon = document.getElementById('ham-menu');
let navBar = document.getElementById('nav-bar');
let navLinks = navBar.querySelectorAll('li');

hamMenuIcon.addEventListener('click', () => {
	navBar.classList.toggle('active');
	hamMenuIcon.classList.toggle('fa-times');
});
navLinks.forEach((navLinks) => {
	navLinks.addEventListener('click', () => {
		navBar.classList.remove('active');
		hamMenuIcon.classList.toggle('fa-times');
	});
});

# 2. 제이쿼리로 구현하기

index.html

...생략
  <!-- <script src="vanilla.js"></script> -->
  <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  <script src="jquery.js"></script>
</body>

jquery.js

let hamMenuIcon = $('#ham-menu');
let navBar = $('#nav-bar');
let navLinks = $('li');

hamMenuIcon.click(function () {
	navBar.toggleClass('active');
	hamMenuIcon.toggleClass('fa-times');
});
navLinks.click(function () {
	navBar.removeClass('active');
	hamMenuIcon.toggleClass('fa-times');
});