在做響應式版面、彈窗置中或滾動效果時,經常需要取得螢幕、視窗與網頁內容的各種寬高數值。JavaScript 提供了多組屬性,可以因應不同情境使用。
window.screen.width 與 window.screen.height 可取得螢幕解析度;availWidth 與 availHeight 則代表可用工作區,通常會扣除工作列等系統介面。
document.body.clientWidth 與 clientHeight 常用來取得可見區域寬高;若需要包含邊框,則可使用 offsetWidth 與 offsetHeight。若要取得完整內容尺寸,則可使用 scrollWidth 與 scrollHeight。
scrollTop 與 scrollLeft 可取得目前捲動距離;window.screenTop 與 window.screenLeft 則與視窗在螢幕上的位置有關。實作時要注意 IE 與其他瀏覽器在 root 元素上的差異。