前端開發中頻繁需要獲取各種尺寸:屏幕分辨率、瀏覽器可視區域、文檔實際大小、滾動偏移量等。原生 JS 與 jQuery 的 API 命名相似但含義不同,極易混淆。本文整理完整速查表,附帶可視化說明與實戰注意事項,助你精準獲取所需尺寸。
document.body 為例)| 屬性 | 含義 | 包含內容 |
|---|---|---|
clientWidth/Height | 可視區域尺寸 | 內容 + padding,**不含** border、滾動條、margin |
offsetWidth/Height | 佈局尺寸 | 內容 + padding + border,**不含** margin |
scrollWidth/Height | 完整內容尺寸 | 內容實際大小(含被滾動隱藏部分)+ padding,**不含** border、margin |
scrollLeft/Top | 滾動偏移量 | 內容被卷去的水平/垂直距離 |
| 屬性 | 含義 | 備註 |
|---|---|---|
window.innerWidth/Height | 視口尺寸 | 含垂直滾動條寬度(若存在),**標準首選** |
document.documentElement.clientWidth/Height | 視口尺寸 | 不含滾動條,IE 標準模式下等同 innerWidth |
document.body.clientWidth/Height | Body 可視尺寸 | 怪異模式下生效,標準模式建議用 documentElement |
window.scrollX / pageXOffset | 頁面水平滾動距離 | 現代瀏覽器推薦 scrollX |
window.scrollY / pageYOffset | 頁面垂直滾動距離 | 現代瀏覽器推薦 scrollY |
screen.width/height | 屏幕分辨率 | 物理屏幕像素 |
screen.availWidth/Height | 屏幕可用工作區 | 扣除任務欄等系統 UI |
window.screenX/Y | 瀏覽器窗口相對屏幕位置 | 原 screenLeft/Top 非標準 |
| 方法 | 對應原生 | 含義 | 包含內容 |
|---|---|---|---|
$(window).width()/height() | window.innerWidth/Height | 視口尺寸 | 不含滾動條 |
$(document).width()/height() | document.documentElement.scrollWidth/Height | 文檔完整尺寸 | 內容實際大小 |
$(element).width()/height() | element.clientWidth/Height | 元素內容尺寸 | 僅 content |
$(element).innerWidth/Height() | — | 元素內部尺寸 | content + padding |
$(element).outerWidth/Height() | element.offsetWidth/Height | 元素外部尺寸 | content + padding + border |
$(element).outerWidth/Height(true) | — | 元素完整外部尺寸 | content + padding + border + margin |
// 推薦:現代瀏覽器
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// jQuery 等效
const vpW = $(window).width();
const vpH = $(window).height();
function isInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
const docHeight = Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight
);
const scrollTop = window.scrollY || document.documentElement.scrollTop;
const clientHeight = window.innerHeight || document.documentElement.clientHeight;
if (scrollTop + clientHeight >= docHeight - 50) {
// 觸底加載更多
}
// 相對文檔(含滾動)
const { top, left } = element.getBoundingClientRect();
const docTop = top + window.scrollY;
const docLeft = left + window.scrollX;
// jQuery 等效
const offset = $(element).offset(); // { top, left } 相對文檔
const position = $(element).position(); // { top, left } 相對定位父元素
document.body 尺寸行為異常,**務必聲明 <!DOCTYPE html>**border-box 下 width/height 包含 padding/border,但 JS 尺寸 API 返回值不受其影響,始終基于 content-box 計算。window.devicePixelRatio > 1 時,CSS 像素 ≠ 物理像素,screen.width 返回物理像素,innerWidth 返回 CSS 像素。innerWidth 與 clientWidth 差值不一致。掌握尺寸 API 的核心在於理解「盒模型層級」與「參考系」。日常開發中:
window.innerWidth/Height 或 $(window).width/height()$(document).width/height()element.getBoundingClientRect()(最全面、高性能)window.scrollX/Y建議收藏本表,遇到尺寸問題查表對照,避免試錯浪費時間。