五大寬度獲取方式對照表
| 簡稱 | 原生 JS | jQuery | 包含內容 | 返回值類型 |
| width | el.style.width | $el.width() | content-box 內容區寬度 | 數值 (無單位) |
| innerWidth | el.clientWidth | $el.innerWidth() | content + padding | 數值 |
| outerWidth | el.offsetWidth | $el.outerWidth() | content + padding + border | 數值 |
| outerWidth(true) | - | $el.outerWidth(true) | content + padding + border + margin | 數值 |
| scrollWidth | el.scrollWidth | - | 實際內容寬度 (含溢出) | 數值 |
數學關係公式
width = content width
clientWidth = width + paddingLeft + paddingRight
offsetWidth = clientWidth + borderLeft + borderRight
outerWidth(true) = offsetWidth + marginLeft + marginRight
scrollWidth = max(content 實際寬度, clientWidth)
代碼驗證示例
<div id="box" style="width: 200px; padding: 10px; border: 5px solid red; margin: 20px;">內容</div>
<script>
var el = document.getElementById('box');
var $el = $('#box');
console.log('style.width:', el.style.width); // "200px" (字符串,僅內聯樣式)
console.log('offsetWidth:', el.offsetWidth); // 230 (200+10*2+5*2)
console.log('clientWidth:', el.clientWidth); // 220 (200+10*2)
console.log('scrollWidth:', el.scrollWidth); // 220 (內容未溢出)
console.log('$el.width():', $el.width()); // 200
console.log('$el.innerWidth():', $el.innerWidth()); // 220
console.log('$el.outerWidth():', $el.outerWidth()); // 230
console.log('$el.outerWidth(true):', $el.outerWidth(true)); // 270 (230+20*2)
</script>
關鍵差異與陷阱
- style.width 僅讀內聯樣式:CSS 類/外部樣式表設置的寬度讀取為空串,需用
getComputedStyle(el).width (返回帶 px 字符串) 或 offsetWidth。
- offsetWidth 為唯讀:無法設置,僅讀取渲染後佈局寬度。
- box-sizing 影響:
content-box (默認):width = content,padding/border 向外擴展。
border-box:width = content + padding + border,content 區域壓縮。
- jQuery
.width() 始終返回 content width,與 box-sizing 無關。
- 隱藏元素:
display:none 時,offsetWidth/clientWidth 為 0。需暫時顯示測量或用 getBoundingClientRect()。
- 小數像素:縮放/高 DPI 下可能返回小數,jQuery 3.0+ 返回浮點數,舊版四捨五入。
getBoundingClientRect() 現代萬能方案
var rect = el.getBoundingClientRect();
console.log(rect.width); // 含 padding + border (等同 offsetWidth)
console.log(rect.height); // 含 padding + border
console.log(rect.left); // 相對視口左上角 X
console.log(rect.top); // 相對視口左上角 Y
console.log(rect.x, rect.y); // 別名
// 注意:返回值為浮點數,含 transform 縮放影響
jQuery 內部實現邏輯 (源碼精簡)
// width()
width: function(elem) {
return elem.getBoundingClientRect().width -
parseFloat(css(elem, 'paddingLeft')) -
parseFloat(css(elem, 'paddingRight')) -
parseFloat(css(elem, 'borderLeftWidth')) -
parseFloat(css(elem, 'borderRightWidth'));
}
// innerWidth()
innerWidth: function(elem) {
return elem.clientWidth; // 原生 clientWidth
}
// outerWidth(includeMargin)
outerWidth: function(elem, margin) {
var val = elem.offsetWidth;
if (margin) {
val += parseFloat(css(elem, 'marginLeft')) + parseFloat(css(elem, 'marginRight'));
}
return val;
}
最佳實踐選型指南
| 需求 | 推薦方法 | 理由 |
| CSS 佈局計算 (content width) | $el.width() / el.clientWidth - padding | 標準盒模型內容區 |
| 元素可視佈局寬度 (含 padding/border) | el.offsetWidth / $el.outerWidth() | 原生最快、無依賴 |
| 含 margin 佔用空間 | $el.outerWidth(true) | jQuery 獨有便利方法 |
| 實際內容寬度 (溢出) | el.scrollWidth | 判斷是否需要滾動條 |
| 獲取/設置 CSS width 值 | el.style.width / $el.width(val) | 僅內聯樣式,響應式佈局用 CSS |
| 跨框架/iframe 尺寸 | getBoundingClientRect() | 統一視口座標系 |