javascript 在 iPad/iPhone 上滚动时获取 scrollTop()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16593660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:16:02  来源:igfitidea点击:

Get scrollTop() while scrolling on iPad/iPhone

javascriptjqueryiosweb-applications

提问by Slevin

I'm trying to get the scrollTop() value while scrolling a website on the iPad/iPhone.

我正在尝试在 iPad/iPhone 上滚动网站时获取 scrollTop() 值。

$(window).scroll(function(){
    console.log($(window).scrollTop());
});

I'm using this code for normal desktop browsers. On Safari with the Mac the console shows every pixel while scrolling. But on the iPad, I only get the value when the scrolling stops.

我将此代码用于普通桌面浏览器。在 Mac 上的 Safari 上,控制台会在滚动时显示每个像素。但是在 iPad 上,我只有在滚动停止时才能获得值。

How can I get every scrollTop value while scrolling even on the iPad?

即使在 iPad 上滚动时,如何获取每个 scrollTop 值?

回答by claytronicon

This limitation changed in iOS 8

这个限制在iOS 8 中改变了

This continues to log $(window).scrollTop() after touchend while the inertia/bounce effect is running:

当惯性/反弹效果运行时,这会在 touchend 之后继续记录 $(window).scrollTop():

$(document).on( 'scroll', function(){
console.log($(window).scrollTop());});

tested on iPad 9.2, iPhone 9.3.4

在 iPad 9.2、iPhone 9.3.4 上测试

回答by iPzard

Have you tried the following?

您是否尝试过以下方法?

$(document).on( 'scroll', function(){
  var currentPosition = $(window).scrollTop();
  console.log(currentPosition);
});

回答by Sourav Purkait

Do not make overflow hidden or scrollTop wont work on safari. Just make overflow: auto;

不要让溢出隐藏或 scrollTop 在 safari 上不起作用。只需使溢出:自动;

html,body {
  overflow-x: auto;
  overflow-y: auto;
}

回答by user3246030

Try using window.pageYOffsetand window.pageXOffset.

尝试使用window.pageYOffsetwindow.pageXOffset