Skip to content

Instantly share code, notes, and snippets.

@benbr-personal
Created April 13, 2016 21:47
Show Gist options
  • Save benbr-personal/d1e06713834220af2b64c55d527febb2 to your computer and use it in GitHub Desktop.
Save benbr-personal/d1e06713834220af2b64c55d527febb2 to your computer and use it in GitHub Desktop.
Simple and lightweight scroll progress bar
<div class="scroll-progress">
<span class="scroll-progress--scrolled"></span>
</div>
const scrollProgress = () => {
const $bar = document.querySelector('.scroll-progress--scrolled');
const max = document.body.scrollHeight - innerHeight;
const percent = (pageYOffset / max) * 100;
$bar.style.width = percent + '%';
};
addEventListener('scroll', scrollProgress);
.scroll-progress {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 5px;
background: lightgrey;
&--scrolled {
position: absolute;
width: 0%;
height: 5px;
background: green;
transition: .25s ease-in-out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment