jquery获取文档高度和窗口高度

jquery获取文档高度和窗口高度,$(document).height()、$(window).height()

$(document).height():整个网页的文档高度

$(window).height():浏览器可视窗口的高度

$(window).scrollTop():浏览器可视窗口顶端距离网页顶端的高度(垂直偏移)

用一句话理解就是:当网页滚动条拉到最低端时,$(document).height() == $(window).height() + $(window).scrollTop()。

当网页高度不足浏览器窗口时$(document).height()返回的是$(window).height()。

不建议使用$("html").height()、$("body").height()这样的高度。

原因:

$("body").height():body可能会有边框,获取的高度会比$(document).height()小;

$("html").height():在不同的浏览器上获取的高度的意义会有差异,说白了就是浏览器不兼容。

$(window).height()值有问题,返回的不是浏览器窗口的高度?

原因:网页没有加上<!DOCTYPE>声明。

js获取页面高度和窗口高度

实际应用:设置内容区域合适的高度

//设置内容区域合适高度
var docH = $(document).height(),
winH = $(window).height(),
headerH = $(".header").outerHeight();
footerH = $(".footer").outerHeight();
if(docH<=winH+4){
$("div.container").height(winH-headerH-footerH-50);
}

注:winH+4 因为IE8下只有4像素偏差

猜你喜欢

转载自www.cnblogs.com/dongdong415/p/10914959.html