방법 - 드롭다운 사이드바
측면 탐색에 드롭다운 메뉴를 추가하는 방법을 알아보세요.
Sidenav의 드롭다운 메뉴
드롭다운 사이드바 만들기
1단계) HTML 추가:
예시
<div class="sidenav">
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#clients">Clients</a>
<a href="#contact">Contact</a>
<button class="dropdown-btn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container">
<a
href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<a href="#contact">Search</a>
</div>
예시 설명
<button>, <a> 또는 <p> 요소와 같은 요소를 사용하여 드롭다운 메뉴를 엽니다.
컨테이너 요소(예: <div>)를 사용하여 드롭다운 메뉴를 만들고 그 안에 드롭다운 링크를 추가합니다. sidenav 내부의 모든 링크에 대해 동일한 스타일을 사용합니다.
2단계) CSS 추가:
예시
/* Fixed sidenav, full height */
.sidenav {
height: 100%;
width: 200px;
position: fixed;
z-index: 1;
top:
0;
left: 0;
background-color: #111;
overflow-x:
hidden;
padding-top: 20px;
}
/* Style the sidenav links and the dropdown button */
.sidenav a,
.dropdown-btn {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
border: none;
background: none;
width:100%;
text-align: left;
cursor: pointer;
outline: none;
}
/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover
{
color: #f1f1f1;
}
/* Main content */
.main {
margin-left: 200px; /* Same as the width of the sidenav */
font-size: 20px; /* Increased text to enable scrolling */
padding:
0px 10px;
}
/* Add an active
class to the active dropdown button */
.active {
background-color: green;
color: white;
}
/* Dropdown container (hidden by default).
Optional: add a lighter background color and some left padding to change the
design of the dropdown content */
.dropdown-container {
display:
none;
background-color: #262626;
padding-left: 8px;
}
/* Optional:
Style the caret down icon */
.fa-caret-down {
float: right;
padding-right: 8px;
}
3단계) 자바스크립트 추가:
예시
//* Loop through all dropdown buttons to toggle between hiding and showing its
dropdown content - This allows the user to have multiple dropdowns without any
conflict */
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click",
function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display
=== "block") {
dropdownContent.style.display
= "none";
} else {
dropdownContent.style.display = "block";
}
});
}
팁: CSS 드롭다운 튜토리얼 로 이동하여 드롭다운 에 대해 자세히 알아보세요.
팁: 클릭 가능한 드롭다운 에 대해 자세히 알아보려면 클릭 가능한 드롭다운으로 이동하세요 .
팁: 탐색 모음에 대해 자세히 알아보려면 CSS 탐색 모음 자습서 로 이동 하세요.
팁: 측면 탐색 자습서 로 이동 하여 닫을 수 있는 측면 탐색을 만드는 방법에 대해 알아보세요.