gpt4 book ai didi

javascript - 如何修复无限滚动 javascript?

转载 作者:行者123 更新时间:2023-11-30 19:26:40 25 4
gpt4 key购买 nike

我正在尝试使用以下脚本无限滚动加载数据。初始数据加载正确但似乎没有检测到滚动。我试过添加警报进行调试,但我似乎找不到问题所在。后端页面 p_load.php 运行良好。知道可能是什么问题吗?

$(document).ready(function() {
var track_load = 0; //total loaded record group(s)
var loading = false; //to prevents multipal ajax loads
var total_groups = 2; //total record group(s)

$('#result').load("p_load.php?t=", {'group_no':track_load}, function() {track_load++;}); //load first group

$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
alert("Detected scrolling"); //not working
if(track_load <= total_groups && loading==false) //there's more data to load
{
loading = true; //prevent further ajax loading
$('.animation_image').show(); //show loading image

//load data from the server using a HTTP POST request
$.post('p_load.php?t=',{'group_no': track_load}, function(data){

$("#result").append(data); //append received data into the element

//hide loading image
$('.animation_image').hide(); //hide loading image once data is received

track_load++; //loaded group increment
loading = false;

}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

alert(thrownError); //alert with HTTP error
$('.animation_image').hide(); //hide loading image
loading = false;

});

}
}
});
});

最佳答案

请尝试:

  $(document).on('scroll', function () {
if(window.scrollY + window.innerHeight >= document.body.scrollHeight) {
//your code
};
});

关于javascript - 如何修复无限滚动 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com