Heim  >  Artikel  >  Web-Frontend  >  js获取浏览器的可视区域尺寸的实现代码_javascript技巧

js获取浏览器的可视区域尺寸的实现代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:58:541238Durchsuche

测试例子:

复制代码 代码如下:

BR>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











结果:

chrome:


FF

OPERA:

SAFARI:

IE9:

IE8

IE7:

IE6

说明:

Chrome/FF/Safari/opera<br>对这些浏览器而言,window有个属性innerWidth/innerHeight包含的是整个文档的可视区域尺寸,注意,这个尺寸是包含滚动条大小的。<br>如果我们不计滚动条的影响,就可以直接使用这两个属性。<br>如果滚动条会影响(比如最大化弹出框),那么应该想另外的办法。<br><br>Document对象是每个DOM树的根,但是它并不代表树中的一个HTML元素,document.documentElement属性引用了作为文档根元素的html标记,document.body属性引用了body标记<br>我们这里获取常见的三个值(scrollWidth、offsetWidth和clientwidth)来比较一下<br><br>document.documentElement.scrollWidth返回整个文档的宽度<br>document.documentElement.offsetWidth返回整个文档的可见宽度<br>document.documentElement.clientwidth返回整个文档的可见宽度(不包含边框),clientwidth = offsetWidth - borderWidth<br>不过一般来说,我们不会给document.documentElement来设置边框,所以这里的clientwidth 与 offsetWidth一致<br><br>document.body.scrollWidth返回body的宽度<br><span style="COLOR: #ff0000">注意,这里的scrollWidth有个不一致的地方,基于webkit的浏览器(chrome和safari)返回的是整个文档的宽度,也就是和document.documentElement.scrollWidth一致,</span><br>opera和FF返回的就是标准的body 的scrollWidth,个人觉得opera和FF算是比较合理的。<br>document.body.offsetWidth返回body的offsetWidth<br>document.body.clientwidth返回body的clientwidth(不包含边框),clientwidth = offsetWidth - borderWidth<br><br>我们看上面的例子,会发现body和documentElement的有些值是相等的,这并不是表示他们是等同的。而是因为当我们没有给body设置宽度的时候,document.body默认占满整个窗口宽度,于是就有:<br>document.body.scrollWidth = document.documentElement.scrollWidth<br>document.body.offsetWidth = document.documentElement.offsetWidth<br>document.body.clientwidth = document.documentElement.clientwidth - document.body.borderWidth(body的边框宽度)<br>当我们给body设置了一个宽度的时候,区别就出来了。<br><br>IE9/IE8<br>这两个差不多,唯一的区别是IE9包含window.innerWidth属性,而IE8不包含window.innerWidth属性。<br>document.documentElement.scrollWidth返回整个文档的宽度,和FF等浏览器一致<br>document.documentElement.offsetWidth返回整个文档的可见宽度(包含滚动条,值和innerWidth一致),注意,这里和FF等浏览器又有点区别。<br>document.documentElement.clientwidth返回整个文档的可见宽度(不包含边框),和FF等浏览器一致。clientwidth = offsetWidth - 滚动条宽度<br><br>document.body.scrollWidth返回body的宽度,<span style="COLOR: #ff0000">注意,这里的scrollWidth和FF等浏览器有点区别,这里并不包括body本身的border宽度。</span><br><span style="COLOR: #ff0000">因此例子中,相比FF少了10px。</span><br>document.body.offsetWidth返回body的offsetWidth,和FF等浏览器一致<br>document.body.clientwidth返回body的clientwidth(不包含边框),和FF等浏览器一致,clientwidth = offsetWidth - borderWidth<br><br>IE7<br>IE7与IE9/IE8的主要区别是<br>第一、document.documentElement.offsetWidth的返回值不一样,<br>参见上面说的,IE9/IE8的document.documentElement.offsetWidth包含滚动条,<span style="COLOR: #ff0000">但是,IE7的document.documentElement.offsetWidth不包含滚动条。</span><br>第二、document.documentElement.scrollWidth返回整个文档的宽度,<span style="COLOR: #ff0000">注意,这里和IE9/IE8、FF等浏览器又有不一致,对于IE9/IE8、FF等浏览器,scrollWidth最小不会小于窗口的宽度,但是在IE下没有这个限制,文档有多小,这个就有多小</span><br>其他倒是挺一致的。<br><br>最后是IE6了<br>IE6的document.documentElement返回值与IE9/IE8没有区别(由此可见,对于document.documentElement,IE7就是个奇葩)。<br><span style="COLOR: #ff0000">话说回来,IE的document.body就是个奇葩,当没有给body设置宽度的时候,body是默认占满整个文档的(注意,其他的浏览器都是占满整个窗口),当然,最小值是整个窗口的大小,就是说body指向了根元素。</span><br>因此,在算上IE6在解析width方面的bug,和其他的浏览器的区别就淋漓尽致了。<br>document.body.scrollWidth返回body的宽度,和IE9/IE8/IE7一致<br>document.body.offsetWidth返回body的offsetWidth,注意,由于body的不同,这里的offsetWidth = scrollWidth + borderWidth<br>document.body.clientwidth返回body的clientwidth(不包含边框)clientwidth = offsetWidth - borderWidth<br><span style="COLOR: #ff0000">另外,有一点和IE7同样,就是document.documentElement.scrollWidth没有最小宽度限制。</span>

总结一下,在标准模式下,我们获取文档可见区域的方法如下:

复制代码 代码如下:

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()
}
}
}

getScrollWith()是获取滚动条尺寸,参见
http://www.jb51.net/article/29036.htm
有什么错误欢迎指出
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn