JavaScript獲取瀏覽器高度和寬度值(documentElement,clientHeight,offsetHeight,scrollHeight,scrollTop,offsetParent,offsetY,innerHeight)


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 ==> 頁面對象寬度(即BODY對象寬度加上Margin寬)

document.documentElement.clientHeight ==> 頁面對象高度(即BODY對象高度加上Margin高)

Opera為: 

document.documentElement.clientWidth ==> 頁面對象寬度(即BODY對象寬度加上Margin寬)

document.documentElement.clientHeight ==> 頁面對象高度(即BODY對象高度加上Margin高)

網頁可見區域寬: document.body.clientWidth
網頁可見區域高: document.body.clientHeight
網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)
網頁可見區域高: document.body.offsetHeight (包括邊線的高)
網頁正文全文寬: document.body.scrollWidth
網頁正文全文高: document.body.scrollHeight
網頁被卷去的高: document.body.scrollTop
網頁被卷去的左: document.body.scrollLeft
網頁正文部分上: window.screenTop
網頁正文部分左: window.screenLeft
屏幕分辨率的高: window.screen.height
屏幕分辨率的寬: window.screen.width
屏幕可用工作區高度: window.screen.availHeight
屏幕可用工作區寬度: window.screen.availWidth

(注意:CSS中的margin屬性,與clientWidth、offsetWidth、clientHeight、offsetHeight均無關)

HTML精確定位:scrollLeft、scrollWidth、clientWidth、offsetWidth

scrollWidth ==> 獲取對象的滾動寬度
scrollHeight ==>  獲取對象的滾動高度
scrollLeft ==> 設置或獲取位於對象左邊界和窗口中目前可見內容的最左端之間的距離( 被卷去的左
scrollTop ==> 設置或獲取位於對象最頂端和窗口中可見內容的最頂端之間的距離( 被卷去的高
offsetLeft ==> 獲取對象相對於版面或由 offsetParent 屬性指定的父坐標的計算左側位置
offsetTop ==> 獲取對象相對於版面或由 offsetTop 屬性指定的父坐標的計算頂端位置
offsetHeight ==> 獲取對象相對於版面或由父坐標 offsetParent 屬性指定的父坐標的高度

event.clientX ==> 相對文檔的水平座標

event.clientY ==> 相對文檔的垂直座標

event.offsetX ==> 相對容器的水平坐標

event.offsetY ==> 相對容器的垂直坐標

document.documentElement.scrollTop ==> 垂直方向滾動的值

event.clientX+document.documentElement.scrollTop ==> 相對文檔的水平座標+垂直方向滾動的量

實現代碼:

01    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
02    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
03    <html xmlns="http://www.w3.org/1999/xhtml">
04    <head>
05    <title>請調整瀏覽器窗口</title> <meta http-equiv="content-type" content="text/html; charset=gb2312">
06    </meta></head>
07    <body>
08    <h2 align="center">請調整瀏覽器窗口大小</h2><hr />
09    <form action="#" method="get" name="form1" id="form1">
10    <!--顯示瀏覽器窗口的實際尺寸-->
11    瀏覽器窗口 的 實際高度: <input type="text" name="availHeight" size="4"/><br />
12    瀏覽器窗口 的 實際寬度: <input type="text" name="availWidth" size="4"/><br />
13    </form>
14    <script type="text/javascript">
15    <!--
16    var winWidth = 0;
17    var winHeight = 0;
18     
19    //函數:獲取尺寸
20    function findDimensions() {
21     
22        //獲取窗口寬度
23        if (window.innerWidth) {
24            winWidth = window.innerWidth;
25        } else if ((document.body) && (document.body.clientWidth)) {
26            winWidth = document.body.clientWidth;
27        }
28     
29        //獲取窗口高度
30        if (window.innerHeight) {
31            winHeight = window.innerHeight;
32        } else if ((document.body) && (document.body.clientHeight)) {
33            winHeight = document.body.clientHeight;
34        }
35     
36        //通過深入Document內部對body進行檢測,獲取窗口大小
37        if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
38            winHeight = document.documentElement.clientHeight;
39            winWidth = document.documentElement.clientWidth;
40        }
41     
42        //結果輸出至兩個文本框
43        document.form1.availHeight.value = winHeight;
44        document.form1.availWidth.value = winWidth;
45    }
46     
47    findDimensions();
48     
49    //調用函數,獲取數值
50    window.onresize = findDimensions;
51     
52    //-->
53    </script>
54    </body>
55    </html>
View Code

附 HTML 測試代碼:

01    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02    <html xmlns="http://www.w3.org/1999/xhtml">
03    <head>
04    <title>屬性值測試</title>
05    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
06    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
07    <style type="text/css">
08    /* reset */
09    body, h1, h2, h3, p, dl, dt, dd, ul, ol, li, button, input, textarea, th, td{margin:0; padding:0;}
10    body{font:12px/1.2 Arial, "宋體"; color:#333;vertical-align: middle;_background:url(about:blank) fixed;_height:100%;background:#FFF;}
11    button, input, select, textarea, label{vertical-align:middle;}
12    img{vertical-align:top; border:none;}
13    ul, ol{list-style:none;}
14    a{text-decoration:none; color:#474747; vertical-align:baseline; cursor:pointer;}
15    a:hover{text-decoration:none; color:#f76f0e;}
16    table{border-collapse:collapse; border-spacing:0;}
17    b{font-weight: normal;}
18     
19    /* test */
20    body{border:10px solid #6F6;}
21    .wrapper{width:800px;height:500px;border:1px solid #F00;margin:0 auto;}
22    .div1{margin-top:50px;border:1px solid #CCC;padding:10px;margin-left:10px;}
23    .div2{margin-top:70px;border:1px solid #00F;}
24    </style>
25    <script type="text/javascript" src="js/jquery.js"></script>
26    <script type="text/javascript">
27     
28    </script>
29    </head>
30    <body>
31        <div class="wrapper" id="wrapper">
32            <div class="div1" id="div1">
33                div1,div1,div1,div1,div1,div1,div1,div1,div1,
34                div1,div1,div1,div1,div1,div1,div1,div1,div1,div1
35            </div>
36            <div class="div2" id="div2">
37                div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,
38                <br/>
39                div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,
40                <br/>
41                div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,
42                <br/>
43                div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,
44                <br/>
45                div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,div2,
46            </div>
47        </div>
48    </body>
49    </html>
View Code

匯總

01    ////////瀏覽器視口的高度
02    function windowHeight() {
03        var myHeight = 0;
04        if (typeof(window.innerHeight) == 'number') {
05            //Non-IE
06            myHeight = window.innerHeight;
07        } else if (document.documentElement && (document.documentElement.clientHeight)) {
08            //IE 6+ in 'standards compliant mode'
09            myHeight = document.documentElement.clientHeight;
10        } else if (document.body && (document.body.clientHeight)) {
11            //IE 4 compatible
12            myHeight = document.body.clientHeight;
13        }
14        return myHeight;
15    }
16    /////////瀏覽器視口的寬度
17    function windowWidth() {
18        var myWidth = 0;
19        if (typeof(window.innerWidth) == 'number') {
20            //Non-IE
21            myWidth = window.innerWidth;
22        } else if (document.documentElement && (document.documentElement.clientWidth)) {
23            //IE 6+ in 'standards compliant mode'
24            myWidth = document.documentElement.clientWidth;
25        } else if (document.body && (document.body.clientWidth)) {
26            //IE 4 compatible
27            myWidth = document.body.clientWidth;
28        }
29        return myWidth;
30    }
31    /**** 瀏覽器垂直滾動位置 ****/
32    function scrollY() {
33        var de = document.documentElement;
34        return window.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
35    }
36     
37    /**** 瀏覽器水平滾動位置 ****/
38    function scrollX() {
39        var de = document.documentElement;
40        return window.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
41    }
42     
43    /**** 當前頁面高度 ****/
44    function pageHeight() {
45        return document.body.scrollHeight;
46    }
47     
48    /**** 當前頁面寬度 ****/
49    function pageWidth() {
50        return document.body.scrollWidth;
51    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM