Page Scroll Progress Bar with CSS and JavaScript

Scroll Progress Bar is an indicator to show the position the user is on the page. The scroll indicator at the top of the page shows how far the page is scrolled. In terms of the user interface, the page scroll progress bar is very useful to display the scrolling percentage in a graphical view. When the user scrolls down, the bar is filled up with the background color to indicate the page scroll percentage in progress effect. At the time of scrolling the page up, the reverse effect is shown on the progress bar.

You can add a scroll progress bar on the webpage with CSS and JavaScript. To make the progress bar attractive use a background color that matched the web page UI. In this example code snippet, we will create a page scroll progress bar using HTML, CSS, and JavaScript.

HTML Code:

<div class="header">
    <h2>Scroll Progress Bar</h2>
    <div class="progress-container">
        <div class="progress-bar" id="proBar"></div>
    </div>  
</div>
  
<div class="body-content">
    <h3>Scroll Down to See the Progress Bar Effect</h3>
    <p>This progress bar is an indicator to show how far the page is scrolled.</p>
    <p>The progress bar effect will reverse on the <b>scroll back</b>.</p>
    <p>This Scroll Progress Bar is <b>responsive</b>! It can work on different screen sizes.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

JavaScript Code:

JavaScript
content_copyCopy
// When the user scrolls the page, execute pageScroll function 
window.onscroll = function() {
    pageScroll();
};

function pageScroll() {
    var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
    var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    var scrolled = (winScroll / height) * 100;
    document.getElementById("proBar").style.width = scrolled + "%";
}

CSS Code:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 28px;
    margin: 0;
}
.header {
    position: fixed;
    top: 0;
    z-index: 1;
    width: 100%;
    background-color: #f1f1f1;
}
.header h2 {
    text-align: center;
}
.progress-container {
    width: 100%;
    height: 8px;
    background: #ccc;
}
.progress-bar {
    height: 8px;
    background: #F5011A;
    width: 0%;
}
.body-content {
    padding: 100px 0;
    margin: 50px auto 0 auto;
    width: 80%;
}

keyboard_double_arrow_up