简体   繁体   English

检测当前是否在Android上滚动网页

[英]Detect if currently scrolling a web page on Android

I am trying to detect when a user has finished scrolling a web page using Javascript on Android. 我试图检测用户何时在Android上使用Javascript滚动网页。 The script I have is working on iPhone and seems correct to me that it should work on Android. 我的脚本正在使用iPhone,对我而言,它应该适用于Android。 Anyway, a snippet: 无论如何,一个片段:

previous = pageYOffset;
interval = setInterval(function() {
    //Has scrolling stopped?
    if(previous == pageYOffset) {
        clearInterval(interval);
        //DO SOMETHING AFTER SCROLL COMPLETE
    } else {
        previous = pageYOffset;
    }
}, 200);

The basic idea, poll the pageYOffset every 200ms, if there had been no change then there is no scrolling happening. 基本思路,每200ms轮询一次pageYOffset,如果没有变化则没有滚动发生。 Otherwise, keep looking. 否则,继续寻找。

As I said, this works on iPhone, so I am assuming it is something to do with Android possibly not updating pageYOffset during a scroll? 正如我所说,这适用于iPhone,所以我假设它与Android有关,可能不会在滚动期间更新pageYOffset?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Note: I went for this route as I could not find a isScrolling property or scrollStop type event. 注意:我选择了这条路线,因为找不到isScrolling属性或scrollStop类型事件。 If I have overlooked one, please do tell me :) 如果我忽略了一个,请告诉我:)


Update: Just tried to use the 'scroll' event to detect this. 更新:刚试过使用'scroll'事件来检测这个。 Mixed results here. 结果好坏参半。 On the Android emulator it was working almost correctly, but was very intermittent on an actual phone (2.1 Sense Hero GSM), ie only 1 in 10 scrolls were detected. 在Android模拟器上,它几乎正常工作,但在实际手机(2.1 Sense Hero GSM)上非常间歇,即仅检测到10个滚动中的1个。

Even when it was 'working' on the emulator it was not firing the scroll event when you scroll 'up' when you are at the top of the page (ie to push up to see the address bar). 即使它在模拟器上“工作”,当您在页面顶部时向上滚动(即向上推看地址栏)时,它也不会触发滚动事件。 This is a problem as the page has actually been scrolled (changed position) but I am not recieving the event. 这是一个问题,因为页面实际上已滚动(更改位置),但我没有收到该事件。

Note, the iPhone does seem to fire and detect the event correctly (at least in the emulator, no access to device at the moment). 请注意,iPhone似乎确实触发并正确检测到事件(至少在模拟器中,此刻无法访问设备)。

Any ideas? 有任何想法吗?


Update 2: The new 'scroll' event seems to work (to the same extent as the emulator (1.6 and 2.1)) on SOME Android devices. 更新2:新的'scroll'事件似乎在某些Android设备上起作用(与模拟器(1.6和2.1)相同)。 Will continue to investigate to try to narrow this down. 将继续调查,试图缩小此范围。

Still the issue of 'scroll' not being fired when you scroll up to the address bar. 滚动到地址栏时仍然没有触发“滚动”问题。 Might have to have some kind of hybrid solution of 'scroll' event detection and movement polling after a touch. 触摸后可能需要某种“滚动”事件检测和移动轮询的混合解决方案。

Does Using window.onscroll event to detect page/frame scrolling help answer your question? 使用window.onscroll事件来检测页面/框架滚动有助于回答您的问题吗? That poster used the window.onscroll event to detect scrolling, so maybe you can can save the current position and fire off a setTimeout call in the event handler. 该海报使用window.onscroll事件来检测滚动,因此您可以保存当前位置并在事件处理程序中触发setTimeout调用。 If when the call back happens, you are at the same position, then scrolling would have stopped. 如果回调发生时,您处于相同位置,则滚动将停止。

The Hero helpfully interposes itself on occasional events and doesn't report them correctly to javascript. 英雄有助于插入偶尔的事件,并没有正确地向javascript报告。 We discovered a number of these in our Android development. 我们在Android开发中发现了其中的一些。 Sometimes it seems to matter which way you are holding the phone (not kidding!) 有时你拿着电话的方式似乎很重要(不是开玩笑!)

Maybe you could use onScrollListener? 也许你可以使用onScrollListener?

how to detect Android ListView Scrolling stopped? 如何检测Android ListView滚动停止?

This guy is trying to detect a stop, but i'm sure it works for scrolling as well. 这家伙正试图检测停止,但我确信它也适用于滚动。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM