방법 - 스크롤 시 메뉴 숨기기
CSS 및 JavaScript를 사용하여 아래로 스크롤할 때 탐색 메뉴를 숨기는 방법을 알아보세요.
아래로 스크롤할 때 Navbar를 숨기는 방법
1단계) HTML 추가:
탐색 모음 만들기:
예시
<div id="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
</div>
2단계) CSS 추가:
탐색 모음 스타일 지정:
예시
#navbar {
background-color: #333; /* Black background
color */
position: fixed; /* Make it stick/fixed
*/
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
/* Style the navbar links */
#navbar a {
float: left;
display: block;
color: white;
text-align: center;
padding: 15px;
text-decoration: none;
}
#navbar
a:hover {
background-color: #ddd;
color: black;
}
3단계) 자바스크립트 추가:
예시
/* When the user scrolls down, hide the navbar. When the user scrolls up, show
the navbar */
var prevScrollpos = window.pageYOffset;
window.onscroll =
function() {
var currentScrollPos = window.pageYOffset;
if
(prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
}