JSFiddle - React, Tailwind, and code Playground

by Cameron Bernhardt

HTML

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<span id="counter"><progress id="prog" value="0" max="100"></progress></span>

CSS

p {
    width: 75px;
    overflow: hidden;
    margin-left: auto;
    margin-right: auto;
}
#counter {
    position: fixed;
    right: 25px;
    top: 10px;
    display: block;
}

JavaScript

window.addEventListener("scroll", function (e) {
    var doc = document.documentElement;
    var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

    var body = document.body;
    var height = Math.max(body.scrollHeight, body.offsetHeight,
    doc.clientHeight, doc.scrollHeight, doc.offsetHeight);

    var perc = Math.floor(top / height * 100);
    document.getElementById("prog").setAttribute("value", perc.toString());
}, false);