我发现了一个很好的脚本,可以在用户滚动页面时加载页面内容。本文网址:CLICK HERE
我对这个脚本有一个问题 - 当用户滚动页面 100% 时(当滚动位于底部时)它会运行。我想当滚动到页面的 90% 时启动这个脚本。
原始代码如下:
if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
我这样尝试过:
var percent = 90;
window_scrolled = ((percent/$(document).height())*100);
if($(window).scrollTop() + $(window).height() <= window_scrolled) //user scrolled to bottom of the page?
但是它不起作用。为什么?我的脚本哪里有问题?
最佳答案
完整代码如下:
var percent = 90;
var window_scrolled;
$(window).scroll(function() {
window_scrolled = ($(document).height()/100)*90;
if($(window).scrollTop() + $(window).height() >= window_scrolled) {
alert("scrolled to bottom");
}
});
首先,你的数学计算错了。如果我们说文档高度为 2000px,您的代码将返回 4.5,而不是 1800,因此:
((percent/$(document).height())*100)
变成:
(($(document).height()/100)*percent)
还有,这个:
if($(window).scrollTop() + $(window).height() <= window_scrolled)
变成这样:
if($(window).scrollTop() + $(window).height() => window_scrolled)
关于jQuery - 当窗口滚动 90% 时运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29452778/