Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

scroll progress bar using javascript
ProgrammingJavaScript

How to Create a Scroll Progress Bar with JavaScript

By Admin
November 10, 2024 3 Min Read
0

Adding a scroll progress bar is a great way to enhance user experience, especially on content-rich pages. This simple feature provides users with a visual cue of how far they’ve scrolled through the page and how much content is left to explore. In this article, we’ll walk you through creating a scroll progress bar using HTML, CSS, and JavaScript.

What is a Scroll Progress Bar?

A scroll progress bar is a small, usually horizontal bar that fills up as you scroll down the page. It shows users how much of the page content they’ve viewed and can be especially useful on long articles, blogs, or documentation sites.

Follow this video for complete guidance :

https://www.youtube.com/watch?v=CWF79ahE4_E

Let’s dive into the code!

Step 1: Setting Up HTML

First, we need to create an HTML element for the progress bar. This will be a simple
element positioned at the top of the page.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scroll Progress Bar</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <!-- Progress bar at the top -->
    <div id="progressBar"></div>

    <!-- Page content -->
    <div class="content">
        <h1>Welcome to My Blog!</h1>
        <p>Start scrolling to see the progress bar in action.</p>
        <!-- Add more content here to make the page scrollable -->
    </div>

    <script src="script.js"></script>
</body>
</html>

Step 2: Styling the Progress Bar with CSS

Next, we’ll style the #progressBar element in CSS to make it look like a bar and set it to a fixed position at the top of the page. This way, it will always stay visible as the user scrolls.

Create a file called style.css and add the following styles:

/* Style for the progress bar */
#progressBar {
    position: fixed;
    top: 0;
    left: 0;
    height: 5px;
    width: 0%; /* Initial width is 0% */
    background-color: #4caf50; /* Green color for the progress bar */
    z-index: 99; /* Make sure it stays on top of other elements */
}

/* Additional styling for the page content */
.content {
    padding: 20px;
    max-width: 800px;
    margin: 0 auto;
}

h1 {
    margin-top: 80px;
}

Here, we set the progressBar to a fixed position at the top with an initial width of 0%. As we scroll, we’ll dynamically change this width to reflect the scroll progress.

Step 3: Adding JavaScript to Track Scrolling

Now, let’s use JavaScript to calculate the scroll position and update the width of the progress bar accordingly.

Create a file named script.js and add the following JavaScript code:

// Function to update the scroll progress bar
window.onscroll = function () {
    updateProgressBar();
};

function updateProgressBar() {
    // Get the scroll position and the total scrollable height
    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    let scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;

    // Calculate the scroll percentage
    let scrollPercentage = (scrollTop / scrollHeight) * 100;

    // Update the width of the progress bar based on scroll percentage
    document.getElementById("progressBar").style.width = scrollPercentage + "%";
}

Full Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scroll Progress Bar</title>
    <style>
        /* Style for the progress bar */
        #progressBar {
            position: fixed;
            top: 0;
            left: 0;
            height: 5px;
            width: 0%; /* Initial width is 0% */
            background-color: #4caf50; /* Green color for the progress bar */
            z-index: 99; /* Make sure it stays on top of other elements */
        }

        /* Additional styling for the page content */
        .content {
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }

        h1 {
            margin-top: 80px;
        }
    </style>
</head>
<body>

    <!-- Progress bar at the top -->
    <div id="progressBar"></div>

    <!-- Page content -->
    <div class="content">
        <h1>Welcome to My Blog!</h1>
        <p>Start scrolling to see the progress bar in action.</p>
        <!-- Add more content here to make the page scrollable -->
    </div>

    <script>
        // Function to update the scroll progress bar
        window.onscroll = function () {
            updateProgressBar();
        };

        function updateProgressBar() {
            // Get the scroll position and the total scrollable height
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            let scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;

            // Calculate the scroll percentage
            let scrollPercentage = (scrollTop / scrollHeight) * 100;

            // Update the width of the progress bar based on scroll percentage
            document.getElementById("progressBar").style.width = scrollPercentage + "%";
        }

    </script>
</body>
</html>
Author

Admin

Follow Me
Other Articles
Previous

Traffic Light Simulation Using HTML, CSS, and JavaScript

Everything you need to know for running a YouTube Channel
Next

Everything you need to know to run a YouTube Channel

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme