
提升阅读体验,从此告别迷失感!本文将为您介绍一种通过添加阅读进度条来增强页面导航的便捷方法。进度条会随着您的滚动而动态填充,直观地显示您在页面中的位置,让阅读过程更轻松有趣。
以下代码片段将引导您实现这一功能:
$(document).ready(function() {
if ($('body').hasClass('single')) {
let totalHeight = $('main').outerHeight(true);
let footerHeight = $('footer').outerHeight(true);
let windowHeight = $(window).height();
if (totalHeight > 0) {
$('header').after('');
$(window).scroll(function() {
let scrollPosition = $(window).scrollTop();
let scrollableHeight = totalHeight + footerHeight - windowHeight;
let progress = (scrollPosition / scrollableHeight) * 100;
progress = Math.min(progress, 100);
$('#sf-reading-progress-bar').css('width', progress + '%');
});
}
}
});
代码说明: 这段代码使用jQuery,在页面加载完成后,判断页面类型是否为单篇文章页面($('body').hasClass('single')),然后计算页面主体内容高度、页脚高度和浏览器窗口高度,从而动态计算并更新进度条宽度。
关键词: 阅读进度条,页面导航,用户体验,jQuery










