DOCTYPE聲明對JS獲取窗口寬度和高度的影響

摘注】:如下說法不許確,不是有沒有DOCTYPE聲明,而是不一樣的DOCTYPE聲明對js的處理有影響。javascript

在沒有DOCTYPE聲明的狀況下:html

document.body.clientWidthjava

document.body.clientHeightwindows

爲當前窗口的寬度/高度;瀏覽器

在DOCTYPE聲明之後函數

document.body.clientWidthui

document.body.clientHeight編碼

爲整個頁面的寬度/高度;設計

document.documentElement.clientWidthorm

document.documentElement.clientHeight

爲當前窗口的寬度/高度;

一樣增長DOCTYPE聲明後

document.body.scrollLeft

document.body.scrollTop

要改成

document.documentElement.scrollLeft

document.documentElement.scrollTop


注意保存時,文件的編碼要看好,有時會出現腳本錯誤,是由於編碼的格式問題,如GB2312

------------------------------------------------------------------

在設計頁面時可能常常會用到固定層的位置,這就須要獲取一些html對象的座標以更靈活的設置目標層的座標,這裏可能就會用到document.body.scrollTop等屬性,可是此屬性在xhtml標準網頁或者更簡單的說是帶<!DOCTYPE ..>標籤的頁面裏獲得的結果是0,若是不要此標籤則一切正常,那麼在xhtml頁面怎麼得到body的座標呢,固然有辦法-使用document.documentElement來取代document.body,能夠這樣寫
例:
var top = document.documentElement.scrollTop || document.body.scrollTop;
在javascript裏||是個好東西,除了能用在if等條件判斷裏,還能用在變量賦值上。那麼上例等同於下例。
例:
var top = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
這麼寫能夠獲得很好的兼容性。


---------------------------------------------------
JS獲取瀏覽器窗口鉅細 獲取屏幕,瀏覽器,網頁高度寬度

網頁可見區域寬: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


HTML精肯定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
scrollHeight: 獲取東西的滾動高度。
scrollLeft:設置或獲取位於東西左界限和窗口中目前可見內容的最左端之間的距離
scrollTop:設置或獲取位於東西最頂端和窗口中可見內容的最頂端之間的距離
scrollWidth:獲取東西的滾動寬度
offsetHeight:獲取東西相對付版面或由父座標 offsetParent 屬性指定的父座標的高度
offsetLeft:獲取東西相對付版面或由 offsetParent 屬性指定的父座標的計算左側位置
offsetTop:獲取東西相對付版面或由 offsetTop 屬性指定的父座標的計算頂端位置
event.clientX 相對文檔的水平座標
event.clientY 相對文檔的筆直座標
event.offsetX 相對容器的水平座標
event.offsetY 相對容器的筆直座標
document.documentElement.scrollTop 筆直偏向滾動的值
event.clientX document.documentElement.scrollTop 相對文檔的水平座標筆直偏向滾動的量

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

(須要提一下:CSS中的margin屬性,與clientWidth、offsetWidth、clientHeight、offsetHeight均無關)

網頁可見區域寬: 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


-------------------

技術要點
本節源代碼主要使用了Document東西關於窗口的一些屬性,這些屬性的主要效用和用法以下。

要獲得窗口的尺寸,對付差異的瀏覽器,須要使用差異的屬性和要領:若要檢測窗口的真實尺寸,在Netscape下須要使用Window的屬性;在IE下需 要深刻Document內部對body進行檢測;在DOM環境下,若要獲得窗口的尺寸,須要注重根元素的尺寸,而不是元素。

Window東西的innerWidth屬性包括當前窗口的內部寬度。Window東西的innerHeight屬性包括當前窗口的內部高度。

Document東西的body屬性對應HTML文檔的標籤。Document東西的documentElement屬性則體現HTML文檔的根節點。

document.body.clientHeight體現HTML文檔所在窗口確當前高度。document.body. clientWidth體現HTML文檔所在窗口確當前寬度。

實現源代碼

<!------------------------------文件名:30.3.htm------------------------------><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>請調解瀏覽器窗口</title><meta http-equiv="content-type" content="text/html; charset=gb2312"></head><body><h2 align="center">請調解瀏覽器窗口鉅細</h2><hr><form action="#" method="get" name="form1" id="form1"><!--顯示瀏覽器窗口的實際尺寸-->瀏覽器窗口的實際高度: <input type="text" name="availHeight" size="4"><br>瀏覽器窗口的實際寬度: <input type="text" name="availWidth" size="4"><br></form><script type="text/javascript"><!-- var winWidth = 0; var winHeight = 0; function findDimensions() //函數:獲取尺寸 { //獲取窗口寬度 if (windows.innerWidth) winWidth = windows.innerWidth; else if ((document.body) && (document.body.clientWidth)) winWidth = document.body.clientWidth; //獲取窗口高度 if (windows.innerHeight) winHeight = windows.innerHeight; else if ((document.body) && (document.body.clientHeight)) winHeight = document.body.clientHeight; //經過深刻Document內部對body進行檢測,獲取窗口鉅細 if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth) { winHeight = document.documentElement.clientHeight; winWidth = document.documentElement.clientWidth; } //結果輸出至兩個文本框 document.form1.availHeight.value= winHeight; document.form1.availWidth.value= winWidth; } findDimensions(); //挪用函數,獲取數值 window.onresize=findDimensions;//--></script></body></html>
源程序解讀

(1)程序首先創建一個表單,包括兩個文本框,用於顯示窗口當前的寬度和高度,而且,其數值會隨窗口鉅細的轉變而轉變。

(2)在隨後的JavaScript源代碼中,首先界說了兩個變量winWidth和winHeight,用於留存窗口的高度值和寬度值。

(3)而後,在函數findDimensions ( )中,使用windows.innerHeight和windows.innerWidth獲得窗口的高度和寬度,並將兩者留存在前述兩個變量中。

(4)再經過深刻Document內部對body進行檢測,獲取窗口鉅細,並存儲在前述兩個變量中。

(5)在函數的最後,經過按名稱訪問表單位素,結果輸出至兩個文本框。

(6)在JavaScript源代碼的最後,經過挪用findDimensions ( )函數,完成整個操縱。

相關文章
相關標籤/搜索