首頁  >  文章  >  web前端  >  javascript取得網頁寬高方法總結_javascript技巧

javascript取得網頁寬高方法總結_javascript技巧

WBOY
WBOY原創
2016-05-16 15:49:491243瀏覽

document.body.clientWidth - 網頁可見區域寬
document.body.clientHeight - 網頁可見區高

document.body.offsetWidth - 網頁可見區域寬,包括邊線和捲軸的寬
document.body.offsetHeight - 網頁可見區域高,包括邊線和捲軸的高[FF,chrom下是整個頁面高,IE opera 下正常]

document.body.scrollWidth - 網頁總寬
document.body.scrollHeight - 網頁總高

document.body.scrollTop - 有捲軸的時候,向下拖曳捲軸,上方不顯示的那部分高度
document.body.scrollLeft - 同上

window.innerHeight - 瀏覽器視窗的內部高度

window.innerWidth - 瀏覽器視窗的內部寬度

window.screenTop - 網頁正文部分上[網頁文檔的最上方距離屏幕最上方的距離,但FF不支持,Chrom,IE,Opera表現都不同,慎用]
window.screenLeft - 網頁正文部分左[網頁文檔的最左方距離屏幕最左方的距離,但FF不支持,Chrom,IE,Opera表現都不同,慎用]

window.screen.height - 螢幕解析度的高度
window.screen.width - 螢幕解析度的寬度

window.screen.availHeight - 可用工作區高度[整個畫面但不包括下方工作列]
window.screen.availWidth - 可用工作區寬度[整個螢幕的寬度]

window.screen.clorDepth - 螢幕色彩,常用的16,32位元等
window.screen.deviceXDPI - 螢幕像素/英吋【IE支持,其它不支援】

JavaScript 取得頁面寬高的方法

<script>
function getInfo()
{
  var s = "";
  s += " 网页可见区域宽:"+ document.body.clientWidth;
  s += " 网页可见区域高:"+ document.body.clientHeight;
  s += " 网页可见区域宽:"+ document.body.offsetWidth + " (包括边线和滚动条的宽)";
  s += " 网页可见区域高:"+ document.body.offsetHeight + " (包括边线的宽)";
  s += " 网页正文全文宽:"+ document.body.scrollWidth;
  s += " 网页正文全文高:"+ document.body.scrollHeight;
  s += " 网页被卷去的高(ff):"+ document.body.scrollTop;
  s += " 网页被卷去的高(ie):"+ document.documentElement.scrollTop;
  s += " 网页被卷去的左:"+ document.body.scrollLeft;
  s += " 网页正文部分上:"+ window.screenTop;
  s += " 网页正文部分左:"+ window.screenLeft;
  s += " 屏幕分辨率的高:"+ window.screen.height;
  s += " 屏幕分辨率的宽:"+ window.screen.width;
  s += " 屏幕可用工作区高度:"+ window.screen.availHeight;
  s += " 屏幕可用工作区宽度:"+ window.screen.availWidth;
  s += " 你的屏幕设置是 "+ window.screen.colorDepth +" 位彩色";
  s += " 你的屏幕设置 "+ window.screen.deviceXDPI +" 像素/英寸";
  //alert (s);
}
getInfo();
</script>

在我本地測試當中:
在IE、FireFox、Opera下都可以使用

document.body.clientWidth
document.body.clientHeight

即可取得,很簡單,很方便。
而在公司專案當中:
Opera仍然使用

document.body.clientWidth
document.body.clientHeight

可是IE和FireFox則使用

document.documentElement.clientWidth
document.documentElement.clientHeight

原來是W3C的標準在作怪啊

a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
如果在頁面中加入這行標記的話

在IE中:

document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度

在FireFox:

document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度

在Opera中:

document.body.clientWidth ==> 可见区域宽度
document.body.clientHeight ==> 可见区域高度
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)
document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)


而如果沒有定義W3C的標準,則

IE為:

document.documentElement.clientWidth ==> 0
document.documentElement.clientHeight ==> 0

FireFox為:

複製程式碼 程式碼如下:
document.documentElement.clientWidth ==> 頁面物件寬度(即BODYDY Margin寬)
document.documentElement.clientHeight ==> 頁面物件高度(即BODY物件高度加上Margin高)

Opera為:

複製程式碼 程式碼如下:
document.documentElement.clientWidth ==> 頁物件寬度(即DYDYDY寬度加上Margin寬)
document.documentElement.clientHeight ==> 頁面物件高度(即BODY物件高度加上Margin高)

以上所述就是本文的全部內容了,希望大家能夠喜歡。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn