移动Safari $(window).height()URL栏差异 [英] Mobile Safari $(window).height() URL bar discrepancy

查看:101
本文介绍了移动Safari $(window).height()URL栏差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将固定高度div设置为窗口高度的100%.但是,移动浏览器无法检测到正确的窗口高度.它总是认为URL栏是等式的一部分.

这是我目前正在使用的内容,但不能说明网址栏.

$(function(){
    $(document).ready(function(){ // On load
        alert($.browser);   
        $('#right-sidebar').css({'height':(($(window).height()))+'px'});
    });
    $(window).resize(function(){ // On resize
        $('#right-sidebar').css({'height':(($(window).height()))+'px'});
    });
});

解决方案

Tldr

如果您只想查询窗口高度,跨浏览器并完成它,请使用 jQuery .documentSize 并调用 $.windowHeight() .要实现自己的解决方案,请继续阅读.

何时使用jQuery或文档的clientHeight

jQuery的$(window).height()document.documentElement.clientHeight的包装.它为您提供视口的高度,但不包括浏览器滚动条覆盖的空间.通常,它可以正常工作,并享有几乎通用的浏览器支持.但是在移动设备上,尤其是在iOS中,有问题.

  • 在iOS中,返回值假装URL和选项卡栏可见,即使它们也不可见.一旦用户滚动并且浏览器切换到最小用户界面,它们就会被隐藏.在此过程中,窗口高度大约增加了60px,这在clientHeight(或jQuery)中没有反映出来.

  • clientHeight返回布局视口的大小,而不是可视视口的大小,因此不反映缩放状态.

所以...在移动设备上并不是很好.

何时使用window.innerHeight

还有另一个可以查询的属性:window.innerHeight.

  • 返回窗口高度
  • 基于可视视口,即反映缩放状态,
  • 在浏览器进入最小用户界面(移动Safari)时更新,
  • 但是,它包括滚动条覆盖的区域.

最后一点意味着您不能只将其替换.另外,它在IE8中不受支持,在Firefox FF25之前的版本(2013年10月).

但是它可以用作移动设备的替代品,因为移动浏览器将滚动条显示为临时叠加层,不会占用视口中的空间-在这方面,window.innerHeightd.dE.clientHeight返回相同的值.

跨浏览器解决方案

因此,一个跨浏览器的解决方案可以找出实际的窗口高度,其工作方式如下(伪代码):

IF the size of browser scroll bars is 0 (overlay)
  RETURN window.innerHeight
ELSE
  RETURN document.documentElement.clientHeight

这里的要点是如何确定给定浏览器的滚动条的大小(宽度).您需要为此进行测试.并不是特别困难-看一下我的实现 Ben Alman的原始实现希望.

如果您不想自己动手,也可以使用我的组件- jQuery. documentSize -并通过 $.windowHeight()调用完成. /p>

I'm trying to set a fixed div that's 100% the window height. But mobile safari doesn't detect the correct window height. It always thinks that the URL bar is part of the equation.

This is what I'm using currently but it doesn't account for the URL bar.

$(function(){
    $(document).ready(function(){ // On load
        alert($.browser);   
        $('#right-sidebar').css({'height':(($(window).height()))+'px'});
    });
    $(window).resize(function(){ // On resize
        $('#right-sidebar').css({'height':(($(window).height()))+'px'});
    });
});

解决方案

Tldr

If you just want to query the window height, cross-browser, and be done with it, use jQuery.documentSize and call $.windowHeight(). For implementing your own solution, read on.

When to use jQuery or the clientHeight of the document

jQuery's $(window).height() is a wrapper for document.documentElement.clientHeight. It gives you the height of the viewport, excluding the space covered by browser scroll bars. Generally, it works fine and enjoys near-universal browser support. But there are quirks on mobile, and in iOS in particular.

  • In iOS, the return value pretends that the URL and tab bars are visible, even if they are not. They get hidden as soon as the user scrolls and the browser switches to minimal UI. Window height is increased by roughly 60px in the process, and that is not reflected in the clientHeight (or in jQuery).

  • The clientHeight returns the size of the layout viewport, not the visual viewport, and therefore does not reflect the zoom state.

So... not quite so great on mobile.

When to use window.innerHeight

There is another property you can query: window.innerHeight. It

  • returns the window height,
  • is based on the visual viewport, ie reflects the zoom state,
  • is updated when the browser enters minimal UI (mobile Safari),
  • but it includes the area covered by scroll bars.

The last point means that you can't just drop it in as a replacement. Also, it is not supported in IE8, and broken in Firefox prior to FF25 (October 2013).

But it can be used as a replacement on mobile because mobile browsers present scroll bars as a temporary overlay which does not consume space in the viewport - window.innerHeight and d.dE.clientHeight return the same value in that regard.

Cross-browser solution

So a cross-browser solution, for finding out the real window height, works like this (pseudo code):

IF the size of browser scroll bars is 0 (overlay)
  RETURN window.innerHeight
ELSE
  RETURN document.documentElement.clientHeight

The catch here is how to determine the size (width) of the scroll bars for a given browser. You need to run a test for it. It's not particularly difficult - have a look at my implementation here or the original one by Ben Alman if you wish.

If you don't want to roll your own, you can also use a component of mine - jQuery.documentSize - and be done with a $.windowHeight() call.

这篇关于移动Safari $(window).height()URL栏差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆