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 yo开发者_StackOverflowu 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.
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!)
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.
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.
精彩评论