w3.css
스크롤 막대 아래로 슬라이드
CSS 및 JavaScript를 사용하여 스크롤 시 탐색 막대를 아래로 슬라이드하는 방법을 알아보자.
바를 아래로 슬라이드하는 방법
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: -50px; /* Hide the navbar 50 px outside of the top view */ 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 20px from the top of the document, slide down the navbar // When the user scrolls to the top of the page, slide up the navbar (50px out of the top view) window.onscroll = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("navbar").style.top = "0"; } else { document.getElementById("navbar").style.top = "-50px"; } }기본 예시예제 보기
참고
W3C School - How TO - Slide Down a Bar on Scroll