javascript - 向下滚动时无限滚动不起作用
问题描述
我有一个代码无限滚动,但我有一个问题。为什么我的代码在向上滚动而不是向下滚动时工作?
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function(){
var lastID = $('.load-more').attr('lastID');
if(($(window).scrollTop() == $(document).height() - $(window).height()) && (lastID != 0)){
//Why work when scroll up? i want this work when scroll down
$.ajax({
type:'POST',
url:'<?php echo base_url('berita/load_more'); ?>',
data:'id='+lastID,
beforeSend:function(){
$('.load-more').show();
},
success:function(html){
$('.load-more').remove();
$('#postList').append(html);
}
});
}
});
});
</script>
解决方案
使用此代码重试
function scrollEvt(){
var lastID = $('.load-more').attr('lastID');
if(((window.innerHeight + window.scrollY) >= document.body.offsetHeight) && (lastID != 0)){
$(window).off('scroll');
$.ajax({
type:'POST',
url:'https://tabungamal.id/berita/load_more',
data:'id='+lastID,
beforeSend:function(){
$('.load-more').show();
},
success:function(html){
$('.load-more').remove();
$('#postList').append(html);
$(window).scroll(scrollEvt);
}
});
}
}
$(document).ready(function(){
$(window).scroll(scrollEvt);
});
推荐阅读
- c++ - C ++从模块大小中的进程读取所有内存
- javascript - 重新加载页面后,ajax 没有将我的输入保存在表单中
- swift - 类初始化器 swift ,将向量设为可选
- javascript - Reacht JS:渲染异步创建的数组时未正确应用 CSS
- sql - 如何在 Presto SQL 中取消嵌套数组值的列?
- azure - KeyVaultClient 方法 GetSecret() 安全吗?
- python - 在(Python)中读取 csv 文件时读取上一行
- asp.net-core - “MySQLDbContextOptionsBuilder”不包含“ServerVersion”的定义
- polymer-2.x - 里面的物品
作为参数传递给函数的组件产生空对象 - pyspark - 在生产环境中将数据从 Azure Databricks 插入到 Synapse 时权限被拒绝