JavaScript - 获取浏览器高度 [英] JavaScript - Get Browser Height

查看:220
本文介绍了JavaScript - 获取浏览器高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个代码片段来获取浏览器窗口中可查看区域的高度。

I am looking for a code snippet to get the height of the viewable area within a browser window.

我有这个代码,但它有点像bug身体没有超过窗口的高度然后它回来了。

I had this code, however it is somewhat bugged as if the the body doesn't exceed the height the of the window then it comes back short.

document.body.clientHeight;

我已经尝试了其他一些东西,但它们要么返回NaN,要么与上面相同。

I have tried a couple of other things but they either return NaN or the same height as the above.

有谁知道如何获得浏览窗口的真实高度?

Does anyone know how to get the real height of the browsing window?

推荐答案

你需要这样的东西,取自 http://www.howtocreate .co.uk / tutorials / javascript / browserwindow

You'll want something like this, taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

那么 innerHeight 对于现代浏览器, documentElement.clientHeight 用于IE, body.clientHeight 用于已弃用/怪异。

So that's innerHeight for modern browsers, documentElement.clientHeight for IE, body.clientHeight for deprecated/quirks.

这篇关于JavaScript - 获取浏览器高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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