Created
April 13, 2016 21:47
-
-
Save benbr-personal/d1e06713834220af2b64c55d527febb2 to your computer and use it in GitHub Desktop.
Simple and lightweight scroll progress bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="scroll-progress"> | |
<span class="scroll-progress--scrolled"></span> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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