KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a UIWebView with an HTML page completely loaded. The UIWebView has a frame of 320 x 480 and scrolls horizontally. I can get the current offset a user is currently at. I would like to find the closest anchor using the XY offset so I can "jump to" that anchors position. Is this at all possible? Can someone point me to a resource in Javascript for doing this? <a id="p-1">Text Text Text Text Text Text Text Text Text<a id="p-2">Text Text Text Text Text Text Text Text Text ... Update My super sad JS code: function posForElement(e) { var totalOffsetY = 0; do { totalOffsetY += e.offsetTop; } while(e = e.offsetParent) return totalOffsetY; } function getClosestAnchor(locationX, locationY) { var a = document.getElementsByTagName('a'); var currentAnchor; for (var idx = 0; idx < a.length; ++idx) { if(a[idx].getAttribute('id') && a[idx+1]) { if(posForElement(a[idx]) <= locationX && locationX <= posForElement(a[idx+1])) { currentAnchor = a[idx]; break; } else { currentAnchor = a[0]; } } } return currentAnchor.getAttribute('id'); } Objective-C float pageOffset = 320.0f; NSString *path = [[NSBundle mainBundle] pathForResource:@"GetAnchorPos" ofType:@"js"]; NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; [webView stringByEvaluatingJavaScriptFromString:jsCode]; NSString *execute = [NSString stringWithFormat:@"getClosestAnchor('%f', '0')", pageOffset]; NSString *anchorID = [webView stringByEvaluatingJavaScriptFromString:execute];
Tags (comma-separated)
Save Edits
Cancel