hide iPhone address bar with 100% height

女生的网名这么多〃 提交于 2019-11-30 02:22:07

This solution from Nate Smith helped me: How to Hide the Address Bar in a Full Screen Iphone or Android Web App.

Here's the essential bit:

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);
};

For more details, check out his blog post or the Gist.

Barney

I struggled with this too. Initially I tried a CSS class (.stretch) defining 200% height and overflow visible, then toggling this on the HTML via script before and after the scrollTo. This doesn't work because the computed 100% height refers back to the available viewport dimensions minus all browser chrome (snapping the status bar back into place).

Eventually I had to request specific styles to apply dynamically via the DOM API. To add to your additional snippet:

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);​

However I'd recommend extending Scott Jehl's method, which addresses minor Android/iOS Safari scrollTo differences:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!