How to load the content while user scroll the web page. How to implement this?
Generally speaking, you will need to have some sort of structure like this
....first page of content... ....first page of content... ....first page of content... ....first page of content... ....first page of content... ....first page of content... ....first page of content... <div id="placeHolder"></div>
Then, you will need to detect when you are getting close to the end of the page, and fetch more data
$(window).scroll(function(){ if ($(window).scrollTop() == $(document).height() - $(window).height()){ AddMoreContent(); } }); function AddMoreContent(){ $.post('getMoreContent.php', function(data) { //Assuming the returned data is pure HTML $(data).insertBefore($('#placeHolder')); }); }
You may need to keep a javascript variable called something like lastId
which stores the last displayed id and pass that to the AJAX receiver so it knows which new content to return. Then in your AJAX you could call
$.post('getMoreContent.php', 'lastId=' + lastId, function(data) { //Assuming the returned data is pure HTML $(data).insertBefore($('#placeHolder')); });
I did exactly this on my company's search page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With