views:

743

answers:

4

I'd like to know the x/y offset of the how far the user has "scrolled" within the viewport in mobile safari on the iphone.

Put another way, if I (through javascript) reloaded the current page, I'd like to find the values I'd need to pass into window.scrollTo(...) in order to reposition the document/viewport as it is currently.

window.pageXOffset always reports 0

jquery's $('body').scrollTop() always reports 0

events have a pageX, but this won't account for the scrolling of the page that happens after you release your finger if your gesture was to "flick" the page up/down. Namely, it'll give me a point when the finger leaves the screen, but that doesn't always match where the page will be after it's finished scrolling.

Any pointers?

A: 

Have you tried the pure js way?

document.body.scrollTop
mracoker
document.body.scrollTop doesn't work either. It will work in Safari on the desktop, but not in mobile safari - it reports 0 always.
A: 

window.pageYOffset should give you the current scroll offset. There's pageXOffset if you need it too.

pendor
Unless it has changed in a recent beta, that doesn't work. That was the crux of my original question ... although I grant you I used pageXOffset rather than pageYOffset.
pageYOffset is working in my shipping app right now. I don't use X, though, so that could be the difference.
pendor
+1  A: 

Hey ! Try to go through this link.

or try following code.

[myWebView stringByEvaluatingJavaScriptFromString: @"scrollY"];

actually - I got the answer from this question.

sugar
A: 

Hi ,

You can use the following methods to solve your problem.

For getting the pageOffset:

int pageYOffset = [[webViewObj stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];

For getting the total scroll height of a webpage:

int scrollHeight = [[webViewObj stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];

For scrolling the webpage to a particular offset:

[webViewObj stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d",scrollHeight ]];
Biranchi