简体   繁体   中英

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. The script I have is working on iPhone and seems correct to me that it should work on 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. 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?

Any help is greatly appreciated!

Note: I went for this route as I could not find a isScrolling property or scrollStop type event. If I have overlooked one, please do tell me :)


Update: Just tried to use the 'scroll' event to detect this. 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.

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).

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. 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? 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. 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. We discovered a number of these in our Android development. Sometimes it seems to matter which way you are holding the phone (not kidding!)

Maybe you could use onScrollListener?

how to detect Android ListView Scrolling stopped?

This guy is trying to detect a stop, but i'm sure it works for scrolling as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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