Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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), 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?


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.

like image 940
Adam Heath Avatar asked Jul 21 '10 16:07

Adam Heath


1 Answers

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

like image 77
Michael Mullany Avatar answered Sep 19 '22 17:09

Michael Mullany