一、背景:
页面获取滚动条顶部、底部位置jquery:
①顶部: $(document).scrollTop() == 0
②底部: $(document).scrollTop() + $(window).height() >= $(document).height()
完整jquery代码:
$(window).scroll(function () {
if ($(document).scrollTop() == 0) {
console.log("顶部");
}
if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
console.log("底部");
}
});
二、问题:
但底部位置一直不正确,无论滚动到哪个位置都输出底部;即 $(window).height() = $(document).height() ; 即:当前可见窗口高度 = 整个页面文档高度
三、解决:
是由于标签<html>未加声明<!DOCTYPE>, 加上声明即可:<!DOCTYPE html>