JSFiddle - React, Tailwind, and code Playground
by lollero
HTML
<div id="scroll-progress">
<div>
<span></span>
</div>
</div>
<div id="wrapper">
</div>
CSS
#wrapper {
margin: 500px auto 800px;
width: 100%;
height: 1800px;
background: rgba(0,0,0, .4);
}
#scroll-progress {
position: fixed;
z-index: 5;
right: 0;
left: 0;
bottom: 20px;
width: 100%;
height: 3px;
}
#scroll-progress div {
height: 100%;
padding: 10px;
background: #000;
}
#scroll-progress span {
display: block;
height: 100%;
width: 0;
background: #fff;
-webkit-transition: width 0.1s ease-out;
transition: width 0.1s ease-out;
}
JavaScript
// $(window).load(function() {
var padding = 100,
scrollProgress = $("#scroll-progress span"),
scrollArea = $("#wrapper"),
scrollAreaOffset = scrollArea.offset().top,
scrollAreaHeight = scrollArea.height(),
win = $(window);
win.on("scroll", function() {
var scrollPos = win.scrollTop() + padding,
distance = scrollPos - scrollAreaOffset,
percentage = Math.floor( (distance / scrollAreaHeight ) * 100 ),
width = getWidth( percentage );
scrollProgress.css({ width: width + "%" });
function getWidth( percentage ) {
var w = 0;
if ( percentage > 0 && percentage < 100 ) {
w = percentage;
}
else if ( percentage < 0 ) {
w = 0;
}
else if ( percentage >= 100 ) {
w = 100;
}
return w;
}
}).trigger('resize');
// });