简体   繁体   English

计算用户向下滚动多少像素

[英]Calculating how many pixels user has scrolled down page

I want to know how many pixels from the top a user has scrolled down my page. 我想知道用户从顶部向下滚动了多少像素。 So , the number of pixels ABOVE that can't be seen PLUS the number of pixels viewable in the current viewport. 因此,看不见的上述像素数量加上当前视口中可见的像素数量。

With Jquery I'm using $(window).scrollTop() which is showing 612 pixels once scrolled to bottom of page, but $(document).height() reports a total height of 1276 pixels. 使用Jquery时,我使用的是$(window).scrollTop() ,它在滚动到页面底部时显示612像素,但是$(document).height()报告的总高度为1276像素。

When I reach the bottom of the page the number I'm wanting to know will be 1276. 当我到达页面底部时,我想知道的数字将是1276。

Hope that makes sense. 希望有道理。

It sounds like what you're trying to get is the bottom of the window's current Y offset. 听起来您要获取的是窗口当前Y偏移量的底部。

This can be calculated by summing the window's scrollTop() and innerHeight : 这可以通过将窗口的scrollTop()innerHeight相加来计算:

$(window).scrollTop() + window.innerHeight

 $(window).scroll(function() { $("#scrollTop").text($(window).scrollTop() + window.innerHeight); $("#docHeight").text($(document).height()); }).scroll(); 
 body {height: 2500px;} div {position: fixed; top: 0; left: 0;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <span>scrollTop:</span> <span id="scrollTop"></span> <br> <span>document Height:</span> <span id="docHeight"></span> </div> 

To calculate how much the user has scrolled the page vertically in terms of pixels from the very top, in JavaScript, we would probe either window.pageYOffset , or in older versions of IE, one of several variants of document.body.scrollTop , whichever property is supported: 要计算用户从最顶部开始垂直滚动页面的像素数量,在JavaScript中,我们将探测window.pageYOffset或旧版IE(即document.body.scrollTop的多个变体之一)中的任何一个支持属性:

var scrollTop = window.pageYOffset || (document.documentElement || document.body.parentNode || document.body).scrollTop

Using jQuery instead, the equivalent would be: 改用jQuery,等效项为:

var scrollTop = $(window).scrollTop()

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body> <div style="height:1000px"></div> <p id="output" style="position:fixed; left:0; top:0; padding:10px; font-weight:bold"> You have scrolled the page by: </p> <script> var output = document.getElementById('output') window.addEventListener("scroll", function(){ var scrollTop = window.pageYOffset || (document.documentElement || document.body.parentNode || document.body).scrollTop; output.innerHTML = 'You have scrolled the page by: ' + scrollTop +'px' }, false) </script> <script> /* ### jQuery version below. Uncomment to see: ### */ /* var $output = $('#output') $(window).on('scroll', function(){ var scrollTop = $(window).scrollTop() $output.html( 'You have scrolled the page by: ' + scrollTop +'px' ) }) */ </script> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取用户向下滚动页面的像素数? - How to get the number of pixels a user has scrolled down the page? 使用鼠标滚轮事件在网站上滚动了多少像素? - How many pixels are scrolled on a website with a mouse wheel down event? JQueryMobile:如何启动页面x像素向下滚动? - JQueryMobile: How to start the page x pixels scrolled down? 如何使用 jQuery 获取向下滚动页面的像素数量? - How to get the amount of pixels scrolled down the page with jQuery? 一旦用户向下滚动页面150px,如何将图像显示为菜单项? - How can I display an image as a menu item once the user has scrolled down the page 150px? 当用户已经向下滚动页面时,如何导航到锚标记? - How can I navigate to an anchor tag when user has already scrolled down page? 跟踪用户滚动到页面多深的最实用方法是什么? - What's the most pragmatic way to keep track of how far down the page a user has scrolled? 当用户向下滚动X像素数量时,将一个类添加到div - Adding a class to a div when the user has scrolled down an X amount of pixels 检测从页面上当前位置滚动到多少像素,而不仅仅是文档顶部? - Detect how many pixels scrolled from current position on page, not just top of document? 如何检测用户已在其 iPhone 上滚动到网页底部 - How to detect user has scrolled to bottom of web page on their iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM