$(window).scrollTop()== $(document).height() - $(window).height() [英] Meaning of $(window).scrollTop() == $(document).height() - $(window).height()

查看:756
本文介绍了$(window).scrollTop()== $(document).height() - $(window).height()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  if 

如果用户已经滚动到页面底部并且工作正常。 ($(window).scrollTop()== $(document).height() - $(window).height()){
//做某事
}

问题:

理解为什么从文档的高度减去窗口的高度,然后将其与滚动高度进行比较以确定是否已达到页面的底部。 ($(window).scrollTop()== $(document).height()为什么不是简单的

  ){
//做某事
}

  if($(window).scrollTop()== $(window).height()){$ b $ // do something 


解决方案

这是因为 $(window).scrollTop()返回页面顶部的位置,并且 $(document).height()返回页面底部的位置。因此,您需要减去窗口的高度以获取与之比较的位置,因为如果完全滚动到底部,这会给出页面顶部所在位置。


The following code is used to detect if a user has scrolled to the bottom of the page and it works.

if($(window).scrollTop() == $(document).height() - $(window).height()){
//do something
}

Problem:

I don't understand why you subtract the height of the window from the height of the document, then compare that to the scroll height to determine whether or not the bottom of the page has been reached. Why isn't it simply

if($(window).scrollTop() == $(document).height()){
//do something
}

or

if($(window).scrollTop() ==  $(window).height()){
//do something
}

解决方案

This is because $(window).scrollTop() returns the position of the top of the page, and $(document).height() returns the position of the bottom of the page. Therefore you need to subtract the height of the window to get the position to compare against, as this will give you the position where the top of the page would be if you were fully scrolled to the bottom.

这篇关于$(window).scrollTop()== $(document).height() - $(window).height()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆