jQuery 实现一个文章阅读进度条功能

6次阅读

共计 555 个字符,预计需要花费 2 分钟才能阅读完成。

背景

阅读进度虽然没啥具体的用处,但是我突然想起来了,随便做做也是极好的 ????
获取元素 offset 高度、元素高度、滑动距离就能实现了

代码

var content_offtop = $('.article-content').offset().top;
var content_height = $('.article-content').innerHeight();
$(window).scroll(function () {if (($(this).scrollTop() > content_offtop)) { // 滑动到内容部分
 if (($(this).scrollTop() - content_offtop) <= content_height) { // 在内容部分内滑动
 this.reading_p = Math.round(($(this).scrollTop() - content_offtop) / content_height * 100);
 } else { // 滑出内容部分
 this.reading_p = 100; // 确保进度条铺满
 }
 } else { // 未滑到内容部分
 this.reading_p = 0; // 确保进度条不显示
 }
 $('.reading-bar').css('width', this.reading_p + '%');
});

↑ JavaScript 代码

正文完
 0