前端開發常需取得各種尺寸:螢幕解析度、瀏覽器視窗、文件大小、捲軸位置等。原生 JS 與 jQuery API 命名不同,行為亦有差異。本文整理完整對照表與最佳實踐。
原生 JavaScript 尺寸屬性對照
| 屬性 | 所屬物件 | 含義 | 含邊框/滾動條 |
screen.width | window.screen | 螢幕解析度寬度 | 否 |
screen.height | window.screen | 螢幕解析度高度 | 否 |
screen.availWidth | window.screen | 可用工作區寬度(扣除任務列) | 否 |
screen.availHeight | window.screen | 可用工作區高度 | 否 |
innerWidth | window | 瀏覽器視窗可視區寬度(含垂直滾動條) | 含滾動條 |
innerHeight | window | 瀏覽器視窗可視區高度(含水平滾動條) | 含滾動條 |
outerWidth | window | 瀏覽器整個視窗寬度(含工具列、邊框) | 含瀏覽器 UI |
outerHeight | window | 瀏覽器整個視窗高度 | 含瀏覽器 UI |
document.documentElement.clientWidth | document.documentElement | 文件可視區寬度(**不含**滾動條,CSS 布局參考) | 不含滾動條 |
document.documentElement.clientHeight | document.documentElement | 文件可視區高度 | 不含滾動條 |
document.documentElement.scrollWidth | document.documentElement | 文件實際內容總寬度(含溢出) | 含溢出 |
document.documentElement.scrollHeight | document.documentElement | 文件實際內容總高度 | 含溢出 |
document.documentElement.scrollLeft | document.documentElement | 水平捲軸位置 | - |
document.documentElement.scrollTop | document.documentElement | 垂直捲軸位置 | - |
document.body.clientWidth | document.body | Body 可視區寬度(Quirks 模式或無 Doctype 時使用) | 不含滾動條 |
window.screenX / screenY | window | 視窗相對螢幕左上角座標 | - |
jQuery 尺寸方法對照
| 方法 | 對應原生 | 含義 | 含 padding | 含 border | 含 margin |
$(window).width() | window.innerWidth | 視窗可視寬 | - | - | - |
$(window).height() | window.innerHeight | 視窗可視高 | - | - | - |
$(document).width() | document.documentElement.scrollWidth | 文件總寬 | - | - | - |
$(document).height() | document.documentElement.scrollHeight | 文件總高 | - | - | - |
$(element).width() | element.clientWidth | 元素內容寬 | ✅ | ❌ | ❌ |
$(element).height() | element.clientHeight | 元素內容高 | ✅ | ❌ | ❌ |
$(element).innerWidth() | element.clientWidth | 元素內容寬 + padding | ✅ | ❌ | ❌ |
$(element).innerHeight() | element.clientHeight | 元素內容高 + padding | ✅ | ❌ | ❌ |
$(element).outerWidth() | element.offsetWidth | 元素外部寬 + border | ✅ | ✅ | ❌ |
$(element).outerWidth(true) | element.offsetWidth + margin | 元素完整寬 + margin | ✅ | ✅ | ✅ |
$(element).outerHeight() | element.offsetHeight | 元素外部高 + border | ✅ | ✅ | ❌ |
$(element).outerHeight(true) | element.offsetHeight + margin | 元素完整高 + margin | ✅ | ✅ | ✅ |
常見使用場景與正確選擇
| 場景 | 推薦 API | 理由 |
| 響應式斷點判斷 | window.innerWidth / $(window).width() | 對應 CSS Media Query 視窗寬度 |
| 元素是否在視窗內 | element.getBoundingClientRect() | 取得相對視窗座標,配合 `IntersectionObserver` 更佳 |
| 捲軸到底載入更多 | document.documentElement.scrollTop + window.innerHeight >= document.documentElement.scrollHeight - threshold | 標準捲軸位置計算 |
| 固定定位元素定位 | element.getBoundingClientRect() | 相對視窗座標,不受父層 transform 影響 |
| 元素完整佔據空間 | $(el).outerWidth(true) | 含 margin,適合排版計算 |
| Canvas/圖表尺寸設定 | element.clientWidth / clientHeight | 內容區實際像素,配合 `devicePixelRatio` 設定 canvas width/height |
響應式監聽最佳實踐
// 現代:ResizeObserver(元素級)
const ro = new ResizeObserver(entries => {
for (const entry of entries) {
console.log('元素尺寸變更:', entry.contentRect.width, entry.contentRect.height);
}
});
ro.observe(document.querySelector('.chart-container'));
// 視窗級:matchMedia + 事件監聽(取代 resize 事件)
const mql = window.matchMedia('(max-width: 768px)');
function handleChange(e) {
console.log('斷點變更:', e.matches ? 'mobile' : 'desktop');
}
mql.addEventListener('change', handleChange); // 現代 API
// mql.addListener(handleChange); // 舊 API(棄用)
裝置像素比(DPR)與高解析度畫布
const dpr = window.devicePixelRatio || 1;
const canvas = document.getElementById('chart');
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
canvas.style.width = rect.width + 'px';
canvas.style.height = rect.height + 'px';
const ctx = canvas.getContext('2d');
ctx.scale(dpr, dpr); // 繪圖座標系縮放
熟記各尺寸 API 的語意差異,能避免「以為拿到視窗寬其實拿到文件寬」等經典 Bug,並能撰寫精準的響應式與捲動邏輯。