Skip to content

Commit

Permalink
fix: Solve the problem of incorrect reading progress calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyib committed Jan 5, 2020
1 parent 08b2321 commit 114268c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
12 changes: 12 additions & 0 deletions layout/_partials/config.pug
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
});
}
var post_widget = "undefined";
if (theme.post_widget) {
var end_text = (theme.post_widget.end_text && theme.post_widget.end_text.enable) || false;
if (end_text) {
post_widget = JSON.stringify({
end_text: end_text,
});
}
}
var night_mode = "undefined";
if (theme.night_mode) {
night_mode = JSON.stringify({
Expand Down Expand Up @@ -125,6 +136,7 @@ script.
fontawesome: !{ fontawesome },
sidebar: !{ sidebar },
header: !{ header },
post_widget: !{ post_widget },
night_mode: !{ night_mode },
back2top: !{ back2top },
reward: !{ theme.reward.enable },
Expand Down
8 changes: 5 additions & 3 deletions layout/post.pug
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends ./_layout.pug

block content
div#is-post
div.post
- var fa_prefix = theme.fa_prefix || 'fa'

Expand All @@ -17,9 +18,10 @@ block content
footer.post-footer
if theme.post_widget.end_text.enable
div.post-end
span= "------ "
span= __("post.end")
span= " ------"
p
span= "------ "
span= __("post.end")
span= " ------"

if (page.copyright !== false) && (theme.creative_commons.enable && theme.creative_commons.post)
include ./_partials/widgets/copyright.pug
Expand Down
1 change: 0 additions & 1 deletion source/css/_common/components/post/post.styl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@

if (hexo-config('post_widget.end_text.enable')) {
&-end {
margin: 0 0 1rem;
padding: 1rem 0 0;
text-align: center;
color: var(--color-gray-400);
Expand Down
55 changes: 48 additions & 7 deletions source/js/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,58 @@ $(document).ready(function () {

// Update the reading progress lines of post.
function readProgress () {
var $post = $('#content-wrap');
var scrollH = ($post[0] &&
$post[0].getBoundingClientRect().top * -1) || 0;
// Not on post page.
if ($('#is-post').length === 0) {
return;
}

var $post = $('.content');
var postTop = $post.offset().top;
var postEndTop = 0;
var postEndHeight = 0;
var postReadingHeight = 0;
var isEnablePostEnd = false;
var percent = 0;

if (CONFIG.post_widget && CONFIG.post_widget.end_text) {
isEnablePostEnd = true;
}

if (isEnablePostEnd) {
postEndTop = $('.post-end').offset().top;
postEndHeight = $('.post-end').outerHeight();
postReadingHeight = postEndTop - postTop + postEndHeight;
} else {
postEndTop = $('.post-footer').offset().top;
postReadingHeight = postEndTop - postTop;
}

var percent = parseInt((scrollH /
Math.abs($post.height() - $(window).height())) * 100);
var windowHeight = $(window).height();
var postScrollTop = 0;

if ($post.length !== 0) {
postScrollTop =
parseInt($post[0].getBoundingClientRect().top * -1) + windowHeight;
}

var percentNum = Number($('.sidebar-reading-info-num').text());

postReadingHeight = parseInt(Math.abs(postReadingHeight));
percent = parseInt((postScrollTop / postReadingHeight) * 100);
percent = percent > 100 ? 100 : percent < 0 ? 0 : percent;

$('.sidebar-reading-info-num').html(percent);
// Has reached the maximum or minimum
if (
(percent === 0 && percentNum === 0) ||
(percent === 100 && percentNum === 100)
) {
return;
}

$('.sidebar-reading-info-num').text(percent);
$('.sidebar-reading-line').css(
'transform', 'translateX(' + (percent - 100) + '%)'
'transform',
'translateX(' + (percent - 100) + '%)'
);
}

Expand Down

0 comments on commit 114268c

Please sign in to comment.