做前端 layout、浮層定位、返回頂部這類互動時,最常被卡住的就是「這個數字到底代表什麼」。同一個頁面上存在好幾組寬高:可視區、內容區、含邊框區、含滾動條的完整文件區,它們的名字相似卻不等價。下面把這幾組屬性一次講清楚。
| 含義 | 取值方式 |
|---|---|
| 頁面可見區域寬度 | 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 |
scrollHeight:取得元素的滾動高度,也就是內容被撐開後的完整高度。
scrollLeft:設定或取得元素左邊界與目前可見內容最左端之間的距離。
scrollTop:設定或取得元素頂端與可見內容最頂端之間的距離。
scrollWidth:取得元素的滾動寬度。
offsetHeight:元素相對於版面(或由 offsetParent 指定的父座標)的高度。
offsetLeft:元素相對於 offsetParent 的計算左側位置。
offsetTop:元素相對於 offsetParent 的計算頂端位置。
event.clientX / clientY 是相對於「可視區」的座標;event.offsetX / offsetY 則是相對於「事件來源容器」的座標。若要換算成文件座標,需要把捲動量加回來,例如 event.clientX + document.documentElement.scrollTop。
IE 6.0、Firefox 1.06+:
clientWidth = width + padding clientHeight = height + padding offsetWidth = width + padding + border offsetHeight = height + padding + border
IE 5.0 / 5.5:
clientWidth = width - border clientHeight = height - border offsetWidth = width offsetHeight = height
另外要記住一件事:CSS 的 margin 與 clientWidth、offsetWidth、clientHeight、offsetHeight 全都無關,它屬於元素外部的間距,不會被計入這些數值。
不同陣營的取法天生不同:Netscape 家族走 window 上的 innerWidth / innerHeight;IE 需要鑽進 document 內部看 body;而在標準 DOM 環境下,應該看根元素 documentElement,而不是 body。
<!DOCTYPE html>
<html>
<head>
<title>請調整瀏覽器視窗</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<h2 align="center">請調整瀏覽器視窗大小</h2>
<form action="#" method="get" name="form1" id="form1">
<p>視窗實際高度:<input type="text" name="availHeight" size="4"></p>
<p>視窗實際寬度:<input type="text" name="availWidth" size="4"></p>
</form>
<script type="text/javascript">
var winWidth = 0;
var winHeight = 0;
function findDimensions() {
// 先嘗試 Netscape 家族的視窗屬性
if (window.innerWidth) winWidth = window.innerWidth;
else if (document.body && document.body.clientWidth)
winWidth = document.body.clientWidth;
if (window.innerHeight) winHeight = window.innerHeight;
else if (document.body && document.body.clientHeight)
winHeight = document.body.clientHeight;
// 標準 DOM 環境下改看根元素
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)宣告 winWidth 與 winHeight 兩個變數,作為最終结果的暫存槽。
3)在 findDimensions() 中優先讀取 window.innerWidth / innerHeight。
4)若上一步取不到,退回 document.body.clientWidth / clientHeight。
5)最後再判斷一次 documentElement,這是標準模式下的正解。
6)把結果寫進表單欄位,並透過 window.onresize 掛上監聽,讓數字永遠跟得上視窗變化。