我有一个小脚本,允许我的用户接收新项目,而无需重新加载页面或使用分页。
我有一个相当大的页脚,其中包含一些信息,目前此脚本仅在用户到达页面底部时加载。
有没有办法让用户在页面向下移动 90% 时,该功能就会启动?
这是我当前的脚本:
<script type="text/javascript">
alreadyloading = false;
start = 20;
stop = 30;
$(window).scroll(function() {
if ($('body').height() <= ($(window).height() + $(window).scrollTop())) {
if (alreadyloading == false) {
alreadyloading = true;
$('.last').show();
$.ajax({
type: "GET",
url: "/trophy-room/all/",
data: "start="+start+"&stop="+stop,
success: function(data) {
$(data).insertBefore('.last');
start = stop;
stop = stop + 10;
$('.last').hide();
alreadyloading = false;
}
});
// if you reach bottom, send 10 as begging and 20 as stop
// load the data and insert before div last or something. [5:10]
}
}
});
</script>
最佳答案
super 简单。只需将 $('body').height()
乘以 0.9。
<script type="text/javascript">
alreadyloading = false;
start = 20;
stop = 30;
$(window).scroll(function() {
if (($(window).scrollTop + $(window).height()) >= ($('body').height() * 0.9)) {
if (alreadyloading == false) {
alreadyloading = true;
$('.last').show();
$.ajax({
type: "GET",
url: "/trophy-room/all/",
data: "start="+start+"&stop="+stop,
success: function(data) {
$(data).insertBefore('.last');
start = stop;
stop = stop + 10;
$('.last').hide();
alreadyloading = false;
}
});
// if you reach bottom, send 10 as begging and 20 as stop
// load the data and insert before div last or something. [5:10]
}
}
});
</script>
关于javascript - jQuery 在页面下方 90% 处执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5389104/