w3.css
스크롤 표시기
CSS와 JavaScript를 사용하여 스크롤 표시기를 만드는 방법을 알아보자.
스크롤 표시기를 만드는 방법
1단계) HTML 추가
<div class="header"> <h2>Scroll Indicator</h2> <div class="progress-container"> <div class="progress-bar" id="myBar"></div> </div> </div> <div>content...</div>
2단계) CSS 추가
/* Style the header: fixed position (always stay at the top) */ .header { position: fixed; top: 0; z-index: 1; width: 100%; background-color: #f1f1f1; } /* The progress container (grey background) */ .progress-container { width: 100%; height: 8px; background: #ccc; } /* The progress bar (scroll indicator) */ .progress-bar { height: 8px; background: #04AA6D; width: 0%; }
3단계) JavaScript 추가
// When the user scrolls the page, execute myFunction window.onscroll = function() {myFunction()}; function myFunction() { var winScroll = document.body.scrollTop || document.documentElement.scrollTop; var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; var scrolled = (winScroll / height) * 100; document.getElementById("myBar").style.width = scrolled + "%"; }
기본 예시
예제 보기참고
W3C School - How TO - Scroll Indicator