jquery獲取窗口高度和窗口高度,$(document).height()、$(window).height()
$(document).height():整個網(wǎng)頁的文檔高度 $(window).height():瀏覽器可視窗口的高度 $(window).scrollTop():瀏覽器可視窗口頂端距離網(wǎng)頁頂端的高度(垂直偏移) $(document.body).height();//瀏覽器當(dāng)前窗口文檔body的高度 $(document.body).outerHeight(true);//瀏覽器當(dāng)前窗口文檔body的總高度 包括border padding margin $(window).width(); //瀏覽器當(dāng)前窗口可視區(qū)域?qū)挾? $(document).width();//瀏覽器當(dāng)前窗口文檔對象寬度 $(document.body).width();//瀏覽器當(dāng)前窗口文檔body的高度 $(document.body).outerWidth(true);//瀏覽器當(dāng)前窗口文檔body的總寬度 包括border padding margin用一句話理解就是:當(dāng)網(wǎng)頁滾動條拉到最低端時,$(document).height() == $(window).height() + $(window).scrollTop()。
當(dāng)網(wǎng)頁高度不足瀏覽器窗口時$(document).height()返回的是$(window).height()。
不建議使用$("html").height()、$("body").height()這樣的高度。
原因:
$("body").height():body可能會有邊框,獲取的高度會比$(document).height()小;
$("html").height():在不同的瀏覽器上獲取的高度的意義會有差異,說白了就是瀏覽器不兼容。
$(window).height()值有問題,返回的不是瀏覽器窗口的高度?
原因:網(wǎng)頁沒有加上<!DOCTYPE>聲明。
js獲取頁面高度和窗口高度
實際應(yīng)用:設(shè)置內(nèi)容區(qū)域合適的高度
//設(shè)置內(nèi)容區(qū)域合適高度 var docH = $(document).height(), winH = $(window).height(), headerH = $(".header").outerHeight(); footerH = $(".footer").outerHeight(); if(docH<=winH+4){ $("div.container").height(winH-headerH-footerH-50); }