Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

jquery - Mobile Safari $(window).height() URL bar discrepancy

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'});
    });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...