[英]Using jQuery, how can I add a “scroll counter” to my page?
I would like to have a scroll counter on the bottom of my page. 我想在页面底部设置一个滚动计数器。
Something like: "Hey, you have just scrolled 1278px" and have the scroll number update everytime I scroll. 诸如:“嘿,您刚刚滚动了1278px”,并且每次滚动时都会更新滚动号。
A non working snippet to just show what I want to achieve: 一个不起作用的代码段,仅显示我想要实现的目标:
$myPage.on('scroll', function(){
$('#myCounter').html(newScrollValueCalculatedSomehow);
})
To clarify: I don't want to see the scrolled number of pixels but the "total distance traveled". 需要说明的是:我不想看到滚动的像素数,而是“经过的总距离”。 If I keep going up and down with the scroll, I want the counter to just increase and increase.
如果我不断滚动滚动,我希望计数器不断增加。
Here's a quick example: 这是一个简单的示例:
http://jsfiddle.net/xs0o095r/ http://jsfiddle.net/xs0o095r/
Keep a "total scrolled" counter and increment it by abs(current - last) 保持“总滚动”计数器,并将其递增abs(当前-最后)
var scrolled = 0;
var lastScrolled = 0;
$(document).on('scroll', function (evt) {
var pos = $(document).scrollTop();
scrolled += Math.abs(pos - lastScrolled);
lastScrolled = pos;
$('#scrolled').html(scrolled);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.