最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onscroll event not being triggered on iPad after single touch panning? - Stack Overflow

programmeradmin0浏览0评论

In trying to figure out the scroll position for a UIWebView, I'm attaching a listener in the HTML that will call back to the main app. I attach the javascript listener like:

window.onscroll = function reportScroll() {
    var sY = window.pageYOffset;
    alert('Scroll pos: '+sY);  // Would evetually trigger a URL or something
}

This event only seems to be triggered at the end of a flick scroll on OS 3.2 (iPad), once the deceleration has ended. However this: .html#//apple_ref/doc/uid/TP40006511-SW7 seems to indicate that it should be triggered at the end of a single finger pan as well. I really need to know when that pan pletes as well.

In trying to figure out the scroll position for a UIWebView, I'm attaching a listener in the HTML that will call back to the main app. I attach the javascript listener like:

window.onscroll = function reportScroll() {
    var sY = window.pageYOffset;
    alert('Scroll pos: '+sY);  // Would evetually trigger a URL or something
}

This event only seems to be triggered at the end of a flick scroll on OS 3.2 (iPad), once the deceleration has ended. However this: https://developer.apple./library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW7 seems to indicate that it should be triggered at the end of a single finger pan as well. I really need to know when that pan pletes as well.

Share Improve this question edited May 5, 2018 at 14:29 Cœur 38.8k26 gold badges205 silver badges277 bronze badges asked Jul 9, 2010 at 21:48 Matt HallMatt Hall 2,6892 gold badges24 silver badges34 bronze badges 1
  • No, sorry if my ment below wasn't clear - it had no essentially no effect. – Matt Hall Commented Jul 12, 2010 at 20:33
Add a ment  | 

3 Answers 3

Reset to default 3

According to QuirksMode Safari iPhone doesn't fire onscroll event on window, but rather on the document (and any other element). I would bet Safari iPad does the same thing.

I experienced the same problem in iPhone as well: flick scroll correctly produces the onscroll event, but single finger panning does not (I was using this in my fixed menu implementation, where the menu is hidden after ontouchstart event, and restored after onscroll).

I solved the problem by using two parallel events: onscroll and ontouchend. They both refer now to the same event handler (that restores the menu). As events are suppressed during scroll, the ontouchend event does not get fired if the window continues the flick scrolling. Now the event handling works for both flick scroll and panning.

I have not tested this in iPad, I would be interested in knowing if this fix helps in that as well.

In iOS Safari, the onscroll event should be fired only once at the end of the scroll (after deceleration). If you are simply taking your finger off of the screen, think of it as instant deceleration. So if you are performing a 'flick scroll', the onscroll event is still only fired once, at the very end of deceleration.

It sounds like you should also monitor the touchend event:

$(window).on('touchend', function() {
     // Do something
});

Note: The touchend event will be fired for each finger that is lifted off the screen.

发布评论

评论列表(0)

  1. 暂无评论