Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load the web page content based on user scrolling

How to load the content while user scroll the web page. How to implement this?

like image 966
gourav Avatar asked Feb 16 '11 18:02

gourav


1 Answers

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.

like image 194
Dutchie432 Avatar answered Sep 19 '22 02:09

Dutchie432