隐藏100%高度的iPhone地址栏

时间:2011-03-05 21:12:58

标签: javascript iphone hide onload address-bar

关于此的很多帖子,但不完全符合我的情况。我的页面具有灵活的尺寸设置为100%宽度和100%高度,因此典型的有载滚动功能不起作用。有任何想法或其他解决方案吗?

谢谢!

CSS:

* {
    margin:0;
    padding:0;
}
html, body {
    width:100%;
    height:100%;
    min-width:960px;
    overflow:hidden;
}

使用Javascript:

    /mobile/i.test(navigator.userAgent) && !pageYOffset && !location.hash && setTimeout(function () {
  window.scrollTo(0, 1);
    }, 1000);​

2 个答案:

答案 0 :(得分:5)

Nate Smith的这个解决方案帮助了我:How to Hide the Address Bar in a Full Screen Iphone or Android Web App

这是必要的一点:

var page   = document.getElementById('page'),
    ua     = navigator.userAgent,
    iphone = ~ua.indexOf('iPhone') || ~ua.indexOf('iPod');

var setupScroll = window.onload = function() {
  // Start out by adding the height of the location bar to the width, so that
  // we can scroll past it
  if (ios) {
    // iOS reliably returns the innerWindow size for documentElement.clientHeight
    // but window.innerHeight is sometimes the wrong value after rotating
    // the orientation
    var height = document.documentElement.clientHeight;
    // Only add extra padding to the height on iphone / ipod, since the ipad
    // browser doesn't scroll off the location bar.
    if (iphone && !fullscreen) height += 60;
    page.style.height = height + 'px';
  }
  // Scroll after a timeout, since iOS will scroll to the top of the page
  // after it fires the onload event
  setTimeout(scrollTo, 0, 0, 1);
};

有关详细信息,请查看他的blog postGist

答案 1 :(得分:3)

我也在努力解决这个问题。最初我尝试了一个定义200%高度和溢出可见的CSS类(.stretch),然后在scrollTo之前和之后通过脚本在HTML上切换它。这不起作用,因为计算出的100%高度会返回可用的视口尺寸减去所有浏览器镶边(将状态栏捕捉回原位)。

最终我不得不要求通过DOM API动态应用特定样式。要添加到您的其他代码段:

var CSS = document.documentElement.style;

/mobile/i.test(navigator.userAgent) && !pageYOffset && !location.hash && setTimeout(function () {
  CSS.height = '200%';
  CSS.overflow = 'visible';

  window.scrollTo(0, 1);

  CSS.height = window.innerHeight + 'px';
  CSS.overflow = 'hidden';
}, 1000);​

但是我建议扩展Scott Jehl的方法,该方法解决了小型Android / iOS Safari的不同之处:

https://gist.github.com/scottjehl/1183357