views:

2551

answers:

2

I can't seem to capture the scroll event on an iPad. None of these work, what I am doing wrong?

window.onscroll=myFunction;

document.onscroll=myFunction;

window.attachEvent("scroll",myFunction,false);

document.attachEvent("scroll",myFunction,false);

They all work even on Safari 3 on Windows. Ironically, EVERY browser on the PC supports window.onload= if you don't mind clobbering existing events. But no go on iPad.

+4  A: 

The iPhoneOS does capture onscroll events, except not the way you may expect.

One-finger panning doesn’t generate any events until the user stops panning—an onscroll event is generated when the page stops moving and redraws—as shown in Figure 6-1.

Similarly, scroll with 2 fingers fires onscroll only after you've stopped scrolling.

The usual way of installing the handler works e.g.

window.onscroll = function() { alert("Scrolled"); };

(See also http://developer.apple.com/safari/library/documentation/appleapplications/reference/SafariWebContent/HandlingEvents/HandlingEvents.html)

KennyTM
Thanks, that forced me to look further at the problem.The real answer was detecting the top of the viewport on the iphone/ipad only works with `window.pageYOffset` and not `document.body.scrollTop||document.documentElement.scrollTop` like every other browser/hardware in existence.
_ck_
A: 

so what did you do to get it to work?

Ryan
Actually, the scroll event was indeed working, the bug I actually encountered was detecting the true viewport dimensions and location, which make my function not work. The top of the viewport can only be seen via `window.pageYOffset` on the iphone/ipad where no other modern browser (even safari on windows) uses that property.
_ck_