gpt4 book ai didi

javascript - $(document).height() 和 $(window).height() 有什么区别

转载 作者:可可西里 更新时间:2023-11-01 02:49:26 24 4
gpt4 key购买 nike

(希望它不是重复的,因为我在搜索和谷歌搜索时没有找到它)

当滚动条到达后一个 div 的底部时,我试图找到如何在某些固定高度的 div('#div')中进行检测。我应该使用 $(document).height()$(window).height() 来检测这个事件吗?

编辑:我的 div 是固定高度的,我设置了自动滚动,那么如何处理呢?如果我想使用 $('#div').height(),这个高度是固定的....

最佳答案

.height()文档:

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

在您的情况下,听起来您可能需要文档 的高度而不是窗口。可以这样想:window 高度是您所看到的,但是 document 高度包括下方或上方的所有内容。

EXAMPLE

编辑:

scrollTop() 的帮助下检查滚动条的顶部和底部方法:

var bottom = $(document).height() - $(window).height();

$(document).scroll(function(){
var position = $(this).scrollTop();
if (position === bottom) {
console.log("bottom");
}else if(position === 0){
console.log("top");
} else {
console.log("scrolling");
}
});

关于javascript - $(document).height() 和 $(window).height() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14504195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com