I did an example calculating the distance of the loader from the screen. When loader enters the screen, the content will be loaded. In the example I simulate a page with a height of 2000px to illustrate the input and output of the loader and the calculated values. You can view here at jsfiddle .
This template server for you to use the loader
div as a reference. It is also possible to anticipate the space with $('#loader').offset().top - 50
, so the event will be executed 50px before the loader enters the screen. When you load loader, the content will already be loaded, without the user having to reach the end to load more.
I created another jsfiddle with a simple example, but closer to a infinite scroll
, but only as an illustration of how to calculate the input of an element that serves to trigger the event. Tailor your need.
content page 1
content page 2 >
...
Loading next page
JSbase
$(window).scroll(function(){varscrollTop=$(window).scrollTop();varelementOffset=$('#loader').offset().top;vardistance=(elementOffset-scrollTop);if(distance<$(window).height()){//elemento'loader'entrounatela,horadecarregarmaisumapágina$.post('artes4.php',{...},function(data2){//adicionaoconteudonadivcontentmantendoaspáginasanteriores$("#content" ).append( data2 );
});
}
});
It is not a good idea to keep limit = 5 e offset = 0
as parameters for SQL.
Validating these 2 information
You can change and paginate with more records than defined.
I made an update on the jsfiddle and included page numbering as a class attribute - you can use otherwise, change the attribute, hidden field ... The first listing you assign 1 <div id="loader" class="1">
, which corresponds to page 1 of any listing. Later when doing the loader via ajax you assign +1 and have your loader with the reference to page 2, and so on.
JS Increasing Paging As HTML Attribute
$(window).scroll(function() {
var scrollTop = $(window).scrollTop();
var elementOffset = $('#loader').offset().top;
var distance = (elementOffset - scrollTop);
var i = Number( $('#loader').attr('class') ) + 1; // atribui +1 ao loader
if( distance < $(window).height() ){
$('#content').append('<p>Página' + i + '</p>');
$('#loader').attr( 'class' , i )
// elemento 'loader' entrou na tela, hora de carregar mais uma página
// 'i' é a representação da página requisitada
$.post( 'artes4.php' , { pagina: i }, function( data2 ){
// adiciona o conteudo na div content mantendo as páginas anteriores
$( "#content" ).append( data2 );
});
}
});