admin管理员组文章数量:1425674
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.
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?
Share Improve this question edited Aug 15, 2019 at 21:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 21, 2010 at 16:08 Adam HeathAdam Heath 4,7732 gold badges37 silver badges52 bronze badgesUpdate 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.
3 Answers
Reset to default 1The 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.
本文标签: javascriptDetect if currently scrolling a web page on AndroidStack Overflow
版权声明:本文标题:javascript - Detect if currently scrolling a web page on Android - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745426751a2658149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论