我已经与这个问题斗争了很长时间(因为我的插件的错误)并且我找到了如何在 Mobile Safari 中获得适当的窗口高度的方法。
无论缩放级别是多少,它都可以正常工作,而无需减去具有预定义状态栏高度的屏幕高度(将来可能会发生变化)。它适用于 iOS6 全屏模式。
一些测试(在屏幕尺寸为 320x480 的 iPhone 上,横向模式下):
// Returns height of the screen including all toolbars
// Requires detection of orientation. (320px for our test)
window.orientation === 0 ? screen.height : screen.width
// Returns height of the visible area
// It decreases if you zoom in
window.innerHeight
// Returns height of screen minus all toolbars
// The problem is that it always subtracts it with height of the browser bar, no matter if it present or not
// In fullscreen mode it always returns 320px.
// Doesn't change when zoom level is changed.
document.documentElement.clientHeight
以下是检测高度的方法:
var getIOSWindowHeight = function() {
// Get zoom level of mobile Safari
// Note, that such zoom detection might not work correctly in other browsers
// We use width, instead of height, because there are no vertical toolbars :)
var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
// window.innerHeight returns height of the visible area.
// We multiply it by zoom and get out real height.
return window.innerHeight * zoomLevel;
};
// You can also get height of the toolbars that are currently displayed
var getHeightOfIOSToolbars = function() {
var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight();
return tH > 1 ? tH : 0;
};
这种技术只有一个缺点:当页面放大时它不是像素完美的(因为window.innerHeight
总是返回四舍五入的值)。当您在顶部栏附近放大时,它也会返回不正确的值。
自从你问这个问题一年过去了,但无论如何希望这会有所帮助!:)