views:

248

answers:

3

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

Thanks


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), i.e. 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 (i.e. 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 guys?


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.

A: 

Maybe you could use onScrollListener?

http://stackoverflow.com/questions/1768391/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.

Cameron
Here is the android docs for ithttp://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html
Cameron
@Cameron I think that might fall outside scope of the question, the attempt to get whether the page is scrolling is from javascript. Unless the OP is hosting the `WebView` in an application they might not have the proper hooks.
Quintin Robinson
@Cameron I took a look, Quintin is right though, I am only in the web, not an app. This did make me think about trying to use the 'scroll' event though, update posted above.
Adam Heath
+1  A: 

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.

Kevin Hakanson
As far as I am aware this is the same as the scroll event I have been tracking though addEventListener. Getting the start of the scroll is not an issue (in fact I starting my script on every touch of the screen), it is finding out when the scroll has finished.It seems iPhone fires the scroll event at the end of the scroll (perfect), Android emulators and some phones fire it multiple times when scrolling (perfect) although I have found a couple which fire it only once at the start (bad).My current solution is to poll the page to determine when it has stopped moving.Thanks
Adam Heath
+1  A: 

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

Michael Mullany