EN ES
Home > Web development > Javascript Tutorials > How to Build a Progress Bar that Shows Scroll Percentage on the Page

How to Build a Progress Bar that Shows Scroll Percentage on the Page

Diego Cortés
Diego Cortés
September 28, 2024
How to Build a Progress Bar that Shows Scroll Percentage on the Page

In today's digital age, engaging user interfaces are essential for providing an optimal user experience. One feature that can enhance the user experience is a progress bar that reflects the percentage of scrolling on the page. In this article, we will learn how to build a simple progress bar using HTML, CSS, and JavaScript.

What is a Progress Bar?

A progress bar is a visual indicator that shows the progress of a process in a graphical interface. In the context of a web page, a scrolling progress bar indicates how far the user has scrolled through the content of the page.

Benefits of Using a Progress Bar

Enhances User Experience

A progress bar provides visual feedback that can improve the user experience, making users more aware of their position within the content.

Facilitates Navigation

Users can have a better idea of how much content they have left to view, which can aid in decision-making about whether to continue scrolling or take another action.

Increases Engagement

An attractive and functional design can keep users more engaged with the page content, thereby increasing the time they spend on it.

Necessary Components

To build a scrolling progress bar, we will need the following components:

  • HTML: Basic structure of the bar.
  • CSS: Styling to make the bar visible and appealing.
  • JavaScript: Logic to calculate and update the progress bar's position.

Step 1: HTML Structure

Let's start by creating the basic structure of our progress bar. Here’s an example of how to do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Scrolling Progress Bar</title>
</head>
<body>
    <div id="progress-bar"></div>
    <div class="content">
        <h1>Page Content</h1>
        <p>Here you can add a lot of content to test the progress bar...</p>
        <p>Lorem ipsum dolor sit amet...</p>
        <!-- Add more content here -->
    </div>
    <script src="script.js"></script>
</body>
</html>

Step 2: CSS Styling

Next, we will apply the necessary styles to make our progress bar look good. We will create a file named styles.css:

body {
    font-family: Arial, sans-serif;
}

#progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 5px;
    background-color: #4caf50;
    width: 0%;
    z-index: 1000;
}

.content {
    margin-top: 20px;
    padding: 20px;
}

Step 3: JavaScript Logic

Finally, we will implement the logic needed for the progress bar to update as the user scrolls through the page. We will create a file script.js with the following code:

window.onscroll = function() {
    updateProgressBar();
};

function updateProgressBar() {
    const scrollTop = document.documentElement.scrollTop;
    const totalHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    const progress = (scrollTop / totalHeight) * 100;

    document.getElementById('progress-bar').style.width = progress + '%';
}

Explanation of the JavaScript Code

  1. window.onscroll: This event triggers whenever the user scrolls through the page.
  2. updateProgressBar: This function calculates the user's scroll position and updates the width of the progress bar based on the scroll percentage.
  3. scrollTop: Gets the current scroll position of the user on the page.
  4. totalHeight: Calculates the total height of the document by subtracting the viewport height (visible area) from the total height.
  5. progress: This is calculated as the ratio of the current scroll position to the total height, multiplied by 100 to convert it into a percentage.
  6. Bar Style: Finally, we update the style of the bar to reflect the progress.

Conclusion

Creating a progress bar that shows the scroll percentage on a web page is a relatively simple process that significantly enhances the user experience. With just a few steps, you can integrate this functionality using HTML, CSS, and JavaScript. The bar not only provides useful feedback for the user but can also make your page look more elegant and professional.

Now you are ready to implement your own progress bar on your website!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 31.94 ms