HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Learn how to create a scroll indicator with CSS and JavaScript
Step 1) Add HTML:
<div class="header">
<h2>Scroll
Indicator</h2>
<div class="progress-container">
<div class="progress-bar" id="myBar"></div>
</div>
</div>
<div>content...</div>
Step 2) Add 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%;
}
Step 3) Add 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 + "%";
}
Try it Yourself »