kva*_*nce 5 html javascript uiwebview
旋转屏幕后,恢复HTML文档中滚动位置的最佳方法是什么?(这是在Cocoa Touch UIWebView中,但我认为这是一个问题无处不在.)默认行为似乎恢复y偏移(以像素为单位),但由于文本已经重排,因此现在文档中的位置不同.
我最初的想法是:
即使这样有效,我也不希望在文档中插入一堆crud.有没有更好的办法?
这是一个澄清问题的图表.恢复原始y偏移不会产生预期结果,因为在横向模式下更多文本适合于一条线.

不漂亮但有效。这要求整个文档文本中都有跨度标签。
// Return the locator ID closest to this height in pixels.
function findClosestLocator(height) {
var allSpans = document.getElementsByTagName("span");
var closestIdx = 0;
var closestDistance = 999999;
for(var i = 0; i < allSpans.length; i++) {
var span = allSpans[i];
var distance = Math.abs(span.offsetTop - height);
if(distance < closestDistance) {
closestIdx = i;
closestDistance = distance;
}
}
return allSpans[closestIdx].id;
}
Run Code Online (Sandbox Code Playgroud)
旋转后,document.getElementById(spanId).offsetTop是新的 y 偏移,其中spanId是旋转前的结果findClosestLocator()。