This snippet add a reading progress bar across the top of the screen on single posts to indicate how much of the post has been read/is still to read.

Requires Kadence Pro (Elements).

  1. In a fixed Kadence element, add a section within a section.
  2. Style the parent section with a background colour and 5px height. Give an ID of bb-reading-container
  3. Style the child section with a different background colour and 5px height. Give and ID of bb-reading-progress
  4. Assign the element to ‘Fixed top after scroll’ and the show on single posts.
  5. Set ‘Scroll down distance until appears’ to around 100px (depending on header height).
function bb_reading_progress_bar() {
    if (is_single()) {
        ?>
        <script>
document.addEventListener('DOMContentLoaded', function() {
    var progressBar = document.getElementById('bb-reading-progress');

    window.addEventListener('scroll', function() {
        var contentWrap = document.querySelector('.content-wrap .entry-content');
        if (!contentWrap) {
            return;
        }

        var contentHeight = contentWrap.offsetHeight;
        var contentTop = contentWrap.getBoundingClientRect().top + window.scrollY;
        var viewportHeight = window.innerHeight;
        var scrollY = window.scrollY;

        var progress = 0;

        if (scrollY >= contentTop) {
            var scrollEnd = contentTop + contentHeight - viewportHeight;
            progress = (scrollY - contentTop) / (scrollEnd - contentTop) * 100;
            progress = Math.min(progress, 100);
        }

        progressBar.style.width = progress + '%';
    });
});
 </script>
        <?php
    }
}
add_action('wp_footer', 'bb_reading_progress_bar');

Add the PHP to your child theme’s functions.php file, or a code snippets plugin.

Code snippets are offered ‘as is’ and no support is provided. If you have customisation requirements, feel free to contact me for a quote.

Pro Pass Membership

Want access to all current and future child themes, kits, and our B&B Blocks library for Kadence for one low annual price?

Brand & Build Pro Pass

Latest

In the shop

  • Kadence
    Lane Wordpress Theme for KadenceLane Wordpress Theme for Kadence

    for content creators

    £175.00

    View Details 🡢

  • Kadence
    Liora Child Theme for KadenceLiora Child Theme for Kadence

    for bloggers

    £175.00

    View Details 🡢