开发者

onscroll event not being triggered on iPad after single touch panning?

开发者 https://www.devze.com 2023-01-06 11:50 出处:网络
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:

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.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEven开发者_开发技巧ts/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 completes as well.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号