在做響應式版面或動態高度計算時,經常需要取得瀏覽器視窗與文件的尺寸。jQuery 提供了簡單明瞭的方法。
$(document).ready(function () {
alert($(window).height()); // 視窗可視區域高度
alert($(document).height()); // 文件高度
alert($(document.body).height()); // body 高度
alert($(document.body).outerHeight(true)); // body 總高度,含 border、padding、margin
alert($(window).width()); // 視窗可視區域寬度
alert($(document).width()); // 文件寬度
alert($(document.body).width()); // body 寬度
alert($(document.body).outerWidth(true)); // body 總寬度,含 border、padding、margin
});
其中,outerHeight(true) 與 outerWidth(true) 會把邊距也計算進去,適合需要精確外框尺寸的情境。