隐藏 iPhone 地址栏,高度为 100%

发布于 2024-10-21 00:29:40 字数 477 浏览 3 评论 0原文

关于此的很多帖子,但不完全适合我的情况。我的页面的灵活尺寸设置为 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);​

Many posts on this, but not quite for my situation. My page has flexible dimensions set to 100% width and 100% height, so the typical on-load scroll function isn't working. Any thoughts or other solutions?

Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-10-28 00:29:40

Nate Smith 的这个解决方案帮助了我:如何在全屏 Iphone 或 Android Web 应用程序中隐藏地址栏

这是要点:

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

有关更多详细信息,请查看他的 博客文章要点

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.

半山落雨半山空 2024-10-28 00:29:40

我也为此苦苦挣扎。最初,我尝试使用 CSS 类 (.stretch) 定义 200% 高度和溢出可见,然后通过 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文