获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下。

1.IE中,浏览器显示窗口大小只能以下获取: 代码如下复制代码

代码如下

document.body.offsetWidth 
document.body.offsetHeight
 
2.在声明了DOCTYPE的浏览器中,可以用以下来获取浏览器显示窗口大小:

 
document.documentElement.clientWidth 
document.documentElement.clientHeight
 
3.IE,FF,Safari皆支持该方法,opera虽支持该属性,但是返回的是页面尺寸;
同时,除了IE以外的所有浏览器都将此信息保存在window对象中,可以用以下获取: 代码如下复制代码

window.innerWidth 
window.innerHeight
 
4.整个网页尺寸一般获得方法 代码如下复制代码

document.body.scrollWidth 
document.body.scrollHeight
 
5.屏幕分辨率高度一般获得方法 代码如下复制代码

window.screen.height 
window.screen.width
 
6.总结一下实例

function getViewSizeWithoutScrollbar(){//不包含滚动条 
return { 
width : document.documentElement.clientWidth, 
height: document.documentElement.clientHeight 


function getViewSizeWithScrollbar(){//包含滚动条 
if(window.innerWidth){ 
return { 
width : window.innerWidth, 
height: window.innerHeight 

}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){ 
return { 
width : document.documentElement.offsetWidth, 
height: document.documentElement.offsetHeight 

}else{ 
return { 
width : document.documentElement.clientWidth + getScrollWith(), 
height: document.documentElement.clientHeight + getScrollWith() 


}
 
7.IE,FireFox 差异如下:
IE6.0、FF1.06+:

clientWidth = width + padding 
clientHeight = height + padding 
offsetWidth = width + padding + border 
offsetHeight = height + padding + border 
IE5.0/5.5: 
clientWidth = width - border 
clientHeight = height - border 
offsetWidth = width 
offsetHeight = height
 
 
 
8.另附个人最常用的获取整页宽高的方法(需要jquery框架) 代码如下复制代码

$(document).width() < $('body').width() ? $(document).width() : $('body').width(); 
$(document).height() < $('body').height() ? $(document).height() : $('body').height();
 
alert($(window).height()); //浏览器时下窗口可视区域高度
alert($(document).height()); //浏览器时下窗口文档的高度
alert($(document.body).height());//浏览器时下窗口文档body的高度
alert($(document.body).outerHeight(true));//浏览器时下窗口文档body的总高度 包括border padding margin
alert($(window).width()); //浏览器时下窗口可视区域宽度
alert($(document).width());//浏览器时下窗口文档对于象宽度
alert($(document.body).width());//浏览器时下窗口文档body的高度
alert($(document.body).outerWidth(true));//浏览器时下窗口文档body的总宽度 包括border padding margin

alert($(document).scrollTop()); //获取滚动条到顶部的垂直高度
alert($(document).scrollLeft()); //获取滚动条到左边的垂直宽度

js 获取页面可视区域宽高的更多相关文章

  1. JS获取浏览器可视区域的尺寸

    所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度).刚刚使用 document.body.clientHeight 来获取可视区域的高度得到的却是整个文档的高度,然后在cnbl ...

  2. JS 获取浏览器和屏幕宽高信息

    网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body.offsetWidth ...

  3. JS获取浏览器可视区域尺寸

    本文所说的是浏览器窗口的可视区域大小,不是浏览器窗口大小,也非页面尺寸. 在没有声明DOCTYPE的IE中,浏览器显示窗口大小只能以下获取: document.body.offsetWidth doc ...

  4. js获取精确的元素宽高(普通获取高度会有误差)

    当js获取元素宽高时, 并不是一个精确的数字,如果想获取真正的宽高大致方法如下 var oStyle = obj.currentStyle ? obj.currentStyle : window.ge ...

  5. 【转】JS获取浏览器可视区域的尺寸

    from: http://www.xiaoboy.com/detail/1341545044.html 所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度).刚刚使用 docum ...

  6. js获取屏幕以及元素宽高的方法

    一.window相关 网页正文部分上:window.screenTop 网页正文部分左:window.screenLeft 屏幕分辨率的高:window.screen.height 屏幕分辨率的宽:w ...

  7. js 获取屏幕或元素宽高...

    窗口相对于屏幕顶部距离 window.screenTop 窗口相对于屏幕左边距离 window.screenLeft, 屏幕分辨率的高 window.screen.height, 屏幕分辨率的宽 wi ...

  8. JS 获取浏览器和屏幕宽高等信息代码

    JS 获取浏览器和屏幕宽高等信息. 网页可见区域宽:document.body.clientWidth  网页可见区域高:document.body.clientHeight  网页可见区域宽:doc ...

  9. JS获取网页属性包括宽、高等

    JS获取网页属性包括宽.高等. function getInfo()  { // www.jbxue.com var s = "";  s += " 网页可见区域宽:&q ...

随机推荐

  1. cdh 安装记录

    安装文件准备 CDH 下载地址:http://archive.cloudera.com/cdh5/parcels/latest/ 下载操作系统对应的版本: 1.CDH-5.3.0-1.cdh5.3.0 ...

  2. Jenkins配置和使用

    之前整理了Jenkins的下载和安装过程,有需要的可以参考我的博客,地址:   http://www.cnblogs.com/luchangyou/p/5981884.html 接下来整理一下Jenk ...

  3. 【python标准库】内建函数

    abs(x) 返回一个数的绝对值.参数可以是普通的整数,长整数或者浮点数.如果参数是个复数,返回它的模. all(iterable) 如果iterable的所有元素为真(或者iterable为空), ...

  4. java.lang.IllegalArgumentException: View not attached to window manager

    公司项目线上bug: java.lang.IllegalArgumentException: View not attached to window manager at android.view.W ...

  5. IntelliJ IDEA “Finds duplicated code”提示如何关闭

    发现重复的代码这个提示真的很烦啊,我们怎么关闭他呢. 设置在这里: Settings -> Editor -> Inspections -> General -> Duplic ...

  6. Grunt使用教程(限winows)

    前提:安装nodejs 一. 打开dos命令窗口,输入命令 "node -v",确认nodejs安装成功 二. 其次,安装grunt-cli (该插件是grunt命令行插件),执行 ...

  7. stylus or less ?

    为什么不说SASS? 因为它需要安装Ruby,而一般的前端开发人员是不会特地去安装Ruby.我似乎更喜欢nodejs! ok,那么我们怎么在stylus和less 之间做出一个好的选择呢? 首先我本人 ...

  8. vim 撤销 恢复 快捷键

    u   撤销上一步的操作 Ctrl+r 恢复上一步被撤销的操作

  9. 清除HTML标记

    public static string DropHTML(string Htmlstring)        {            if (string.IsNullOrEmpty(Htmlst ...

  10. codefirst mvc Self referencing loop detected for property

    登录时,json序列化用户类时提示错误"Self referencing loop detected for property--",经过5个小时的查找,发现原因可能是,用户类包含 ...