-
-
Save Nikolasgrizli/8c8359c9de7f93a9d85b9ce3c7c76b46 to your computer and use it in GitHub Desktop.
Reading progress
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
$(document).ready(function () { | |
// Start fitVids | |
var $postContent = $(".post-full-content"); | |
$postContent.fitVids(); | |
// End fitVids | |
var progressBar = document.querySelector('#reading-progress'); | |
var header = document.querySelector('.floating-header'); | |
var title = document.querySelector('.post-full-title'); | |
var lastScrollY = window.scrollY; | |
var lastWindowHeight = window.innerHeight; | |
var lastDocumentHeight = $(document).height(); | |
var ticking = false; | |
function onScroll() { | |
lastScrollY = window.scrollY; | |
requestTick(); | |
} | |
function onResize() { | |
lastWindowHeight = window.innerHeight; | |
lastDocumentHeight = $(document).height(); | |
requestTick(); | |
} | |
function requestTick() { | |
if (!ticking) { | |
requestAnimationFrame(update); | |
} | |
ticking = true; | |
} | |
function update() { | |
var trigger = title.getBoundingClientRect().top + window.scrollY; | |
var triggerOffset = title.offsetHeight + 35; | |
var progressMax = lastDocumentHeight - lastWindowHeight; | |
// show/hide floating header | |
if (lastScrollY >= trigger + triggerOffset) { | |
header.classList.add('floating-active'); | |
} else { | |
header.classList.remove('floating-active'); | |
} | |
progressBar.setAttribute('max', progressMax); | |
progressBar.setAttribute('value', lastScrollY); | |
ticking = false; | |
} | |
window.addEventListener('scroll', onScroll, {passive: true}); | |
window.addEventListener('resize', onResize, false); | |
update(); | |
}); |
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="floating-header floating-active"> | |
<progress id="reading-progress" class="progress" value="5932" max="11477"> | |
<div class="progress-container"> | |
<span class="progress-bar"></span> | |
</div> | |
</progress> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment