Reputation: 191
I am trying to make infinite scroll loads data with below script. Initial data loads correctly but seems like no scroll is detected. I've tried adding alert to debug but I can't seem to find the problem. Backend page p_load.php is working fine. Any idea what might be the issue?
$(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;
});
}
}
});
});
Upvotes: 1
Views: 765
Reputation: 21628
You should be using the Intersection Observer API, that is what it is for.
https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
Upvotes: 0
Reputation: 1118
Please try:
$(document).on('scroll', function () {
if(window.scrollY + window.innerHeight >= document.body.scrollHeight) {
//your code
};
});
Upvotes: 1