学而实习之 不亦乐乎

$(window).height() 和 $(document).height()的区别及用法

2019-01-17 15:16:55

$(window).height()和$(document).height的区别

之前做图片懒加载,出现$(window).height()和$(document).height的值是一样的问题。
即$(window).height()和$(document).height返回值一样。这种情况很有可能是因为DOCTYPE没写造成的,请检查html文档,在<html>标签之前加上<!DOCTYPE html>。

$(window).height() 和 $(document).height()的区别
$(window).height()代表了当前可见区域的大小,
$(document).height()则代表了整个文档的高度,可视具体情况使用.
$(document).scrollTop() 获取垂直滚动的距离 (即当前滚动的地方的窗口顶端到整个页面顶端的距离)
$(document).scrollLeft() 这是获取水平滚动条的距离

当浏览器窗口大小改变时(如最大化或拉大窗口后) ,
$(window).height() 随之改变,但是 $(document).height()是不变的。

应用举例

判断页面是否位于顶端
$(this).scrollTop()==0时。

判断页面是否位于底端
scrollTop()>=$(document).height()-$(window).height() 或者 $(document).scrollTop() + $(window).height() >= $(document).height()时