我计算了滚动位置和 html 文档高度的比率,使用
var filled = ($(document).scrollTop() / $(document).height());
我在文档底部得到的是 0.8596615031325553 而不是 1,为什么会这样?? 我必须在我的代码中的其他地方动态地使用这个比率,但由于它给出了意想不到的值,我不能使用它。我该怎么办??
最佳答案
$(document).scrollTop()
返回当前 View 的top距离页面顶部的距离(即用户有多远 < em>向下滚动)。 $(document).height())
返回整个页面的高度,从最顶部的像素到底部的像素。
为了弥补这一点,你可以这样做:
var filled = (($(document).scrollTop() + $(window).height()) / $(document).height());
关于javascript - 为什么 $(document).scrollTop() 不等于文档底部的 $(document).height() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520536/