jQuery实现显示页面滚动进度功能

2022-5-16 18:48| 发布者: Hocassian| 查看: 77| 评论: 0|原作者: 樱花庄的白猫

摘要:
C:\Users\Administrator\Downloads\2019-10-14-0-25-44-134316834848900-樱花庄的白猫 ねこ・しろ・ましろ-采集的数据-后羿采集器.html

标题

jQuery实现显示页面滚动进度功能

标题链接

https://2heng.xin/2017/10/01/jquery-show-scroll-progress/

post-date

发布于 2017-10-01

post-meta

3,032 热度

评价数

3 条评论

分类

野生技术协会

正文

随意写了一个jQuery小工具,DEMO在这页上就有啦~

首先是显示在顶部的进度条,使用以下的CSS:

.scroll-line {
height: 2px;
margin-top: -1em;
background: gray;
width: 0%;
}

以及以下的JS:

$(window).scroll(function(){
var wintop = $(window).scrollTop(),
docheight = $(document).height(),
winheight = $(window).height();
var scrolled = (wintop/(docheight-winheight))*100;
$('.scroll-line').css('width', (scrolled + '%'));
});

通过<div class="scroll-line"></div>调用即可。

然后是右下角的Go to Top按键上的数字,可以沿用上面的JS,稍作修改:

$(window).scroll(function(){
var wintop = $(window).scrollTop(), docheight =
$(document).height(), winheight = $(window).height();
var scrolled = (wintop/(docheight-winheight))*100;
$('.goTOP').each(function () {
var gotop = scrolled.toFixed(0)
$(this).html(gotop + '%');
});
});

然后通过<span class="goTOP"></span>调用。

Q.E.D.

作者

Mashiro


路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部