Home > Web Front-end > CSS Tutorial > Reading Progress Bar

Reading Progress Bar

Susan Sarandon
Release: 2025-01-10 18:04:43
Original
933 people have browsed it

This code snippet adds a reading progress bar to a webpage. The bar visually represents the user's scroll progress through the main content area. Let's break down how it works and improve it.

Reading Progress Bar

The code attempts to calculate the progress based on the scroll position relative to the total height of the main element. However, it has several issues:

  • Incomplete Code: The provided code is truncated. It's missing the crucial part that actually creates and updates the progress bar visually. The div is created, but no styling or progress update logic is present.
  • Incorrect Height Calculation: The calculation of totalHeight might be inaccurate depending on the page structure. Using outerHeight(true) includes margins, which might not be desired. It also doesn't account for dynamic content that might change the height after the initial calculation.
  • Lack of Progress Update: There's no code to update the progress bar as the user scrolls. The calculation is done only once when the document is ready.
  • Footer Height Issue: Subtracting footerHeight might lead to incorrect progress calculation if the footer is fixed or overlaps the main content.

Here's an improved version that addresses these issues:

<code class="language-javascript">$(document).ready(function() {
  if ($('body').hasClass('single')) {
    const progressBar = $('<div id="reading-progress"></div>');
    $('header').after(progressBar);

    let totalHeight = $('main').height(); // Use height() for more accurate calculation
    let windowHeight = $(window).height();

    $(window).on('scroll', function() {
      let scrollTop = $(this).scrollTop();
      let progress = (scrollTop / (totalHeight - windowHeight)) * 100; // Adjust for window height
      progress = Math.min(progress, 100); // Cap progress at 100%
      $('#reading-progress').css('width', progress + '%');
    });
  }
});</code>
Copy after login

This improved code:

  1. Creates a progress bar: It dynamically creates a div with the ID reading-progress to act as the progress bar.
  2. Uses height(): It uses height() instead of outerHeight(true) for a more accurate calculation of the main content height.
  3. Handles Scroll Events: It uses $(window).on('scroll', ...) to continuously update the progress bar's width as the user scrolls.
  4. Calculates Progress Accurately: The progress calculation accounts for the visible window height, preventing the bar from exceeding 100%.
  5. Adds CSS (Needed): You'll need to add CSS to style the #reading-progress div. For example:
<code class="language-css">#reading-progress {
  height: 5px; /* Adjust height as needed */
  background-color: #007bff; /* Adjust color as needed */
  position: fixed;
  top: 0;
  left: 0;
  width: 0; /* Initially 0% width */
  z-index: 1000; /* Ensure it's on top */
}</code>
Copy after login

Remember to include jQuery in your project for this code to work. This revised code provides a more robust and accurate reading progress bar. Further refinements could include handling edge cases and adding more sophisticated styling.

The above is the detailed content of Reading Progress Bar. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template