阅读进度虽然没啥具体的用处,可是我忽然想起来了,随便作作也是极好的 🙂
获取元素 offset 高度、元素高度、滑动距离就能实现了javascript
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 代码css