Skip to content

Commit

Permalink
Make the reading progress smoother (theme-next#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
luo-zhan authored and stevenjoezhang committed Jan 14, 2020
1 parent 8cc79e5 commit ec80c64
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,18 @@ NexT.utils = {
var readingProgressBar = document.querySelector('.reading-progress-bar');
// For init back to top in sidebar if page was scrolled after page refresh.
window.addEventListener('scroll', () => {
var scrollPercent;
if (backToTop || readingProgressBar) {
var docHeight = document.querySelector('.container').offsetHeight;
var winHeight = window.innerHeight;
var contentVisibilityHeight = docHeight > winHeight ? docHeight - winHeight : document.body.scrollHeight - winHeight;
var scrollPercentRounded = Math.round(100 * window.scrollY / contentVisibilityHeight);
scrollPercent = Math.min(scrollPercentRounded, 100) + '%';
}
if (backToTop) {
backToTop.classList.toggle('back-to-top-on', window.scrollY > THRESHOLD);
backToTop.querySelector('span').innerText = scrollPercent;
}
if (readingProgressBar) {
readingProgressBar.style.width = scrollPercent;
var scrollPercent = Math.min(100 * window.scrollY / contentVisibilityHeight, 100);
if (backToTop) {
backToTop.classList.toggle('back-to-top-on', window.scrollY > THRESHOLD);
backToTop.querySelector('span').innerText = Math.round(scrollPercent) + '%';
}
if (readingProgressBar) {
readingProgressBar.style.width = scrollPercent.toFixed(2) + '%';
}
}
});

Expand Down

0 comments on commit ec80c64

Please sign in to comment.