Skip to content

Instantly share code, notes, and snippets.

@martinjinda
Created January 28, 2020 11:54
Show Gist options
  • Save martinjinda/05fbd4a6aa898413fc0ed5ebe6bd47ca to your computer and use it in GitHub Desktop.
Save martinjinda/05fbd4a6aa898413fc0ed5ebe6bd47ca to your computer and use it in GitHub Desktop.
Reading progress bar
if ($('.post').length) {
let winHeight = $(window).height(),
docHeight = $('.post').height(),
postOffset = $('.post').offset().top,
progressBar = $('.progress-container'),
max, value;
max = docHeight - winHeight;
progressBar.attr('max', max);
$(document).on('scroll', function() {
value = $(window).scrollTop() - postOffset;
if (value >= 0) {
progressBar.show();
let width = (value / max) * 100;
width = width + '%';
$('.progress-bar').css({'width': width});
} else {
progressBar.hide();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment