접이식 사이드 네비게이션
Coddy HTML 여정의 JavaScript in Action 섹션에 포함된 레슨 — 27개 중 14번째.
접이식 사이드 네비게이션은 버튼을 클릭하면 화면 옆에서 슬라이드되어 나타나는 숨겨진 메뉴입니다. 이는 공간을 절약하는 동시에 사용자에게 네비게이션 링크에 대한 빠른 접근을 제공하는 데 도움이 됩니다.
사이드 네비게이션과 토글 버튼이 포함된 기본 HTML 구조를 생성합니다:
<button id="toggleNav">☰ Menu</button>
<nav id="sideNav" class="sidenav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>사이드 네비게이션 스타일을 위한 CSS를 추가하세요:
.sidenav {
height: 100%;
width: 0;
position: fixed;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.3s;
padding-top: 60px;
}
.sidenav.open {
width: 250px;
}내비게이션을 토글하기 위해 JavaScript를 추가합니다:
const toggleBtn = document.getElementById('toggleNav');
const sideNav = document.getElementById('sideNav');
toggleBtn.addEventListener('click', function() {
sideNav.classList.toggle('open');
});버튼을 클릭하면 현재 상태에 따라 사이드 네비게이션이 슬라이드되어 나타나거나 사라집니다.
챌린지
쉬움메뉴 버튼을 기능하게 만드세요:
HTML: onclick 속성을 사용하여 메뉴 버튼을 JavaScript 함수에 연결하세요
JavaScript: 사이드 네비게이션의 너비를 "250px"로 설정하여 이를 여는 함수를 작성하세요
직접 해보기
<!DOCTYPE html>
<html>
<head>
<title>Scandinavian Interior Design</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="sideNav" class="sidenav">
<a href="javascript:void(0)" class="close-btn" onclick="closeNav()">X</a>
<a href="#">Home</a>
<a href="#">Philosophy</a>
<a href="#">Materials</a>
<a href="#">Inspiration</a>
<a href="#">Contact</a>
</div>
<header>
<span class="open-btn">☰ Menu</span>
<h1>Scandinavian Interior Design</h1>
</header>
<main>
<section>
<h2>Calm, Functional, Beautiful</h2>
<p>
Scandinavian design is about more than looks — it’s about balance,
comfort, and harmony. Spaces are uncluttered, airy, and filled with
light, making homes feel both inviting and practical.
</p>
</section>
<section>
<h2>Natural Materials</h2>
<p>
Wood, stone, wool, and linen are at the heart of Scandinavian interiors.
These natural elements bring warmth and a sense of connection to nature,
while ensuring that every item is purposeful and timeless.
</p>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.