Simple Content Load on Scroll

/ Published in: jQuery
Save to your folder(s)

Simple jQuery script to load additional content when you near the bottom of the page, same sorta thing Twitter does (although I originally programed it for a different use). Only thing of remark is the fact that it stores the current highest loaded value in a hidden variable, that way, fi the scrip dies or the user does not ave javascript enabled you can use that variable to load the next set of content up through traditions POST/GET methods.

I also have a version of the script that does not use jQuery if anybody is interested (although it is much larger and not as smooth).

Example is available at the link.

UPDATE: A commenter (on my site at the link) noted that there was a problem with the Content load on Scroll with jQuery example in Firefox 5 for windows and linux, and before I could even get off work to take a look he posted a fix.

The problem has to do with the browsers cacheing system. When you reload the page it keeps the cached value of the hidden input that keeps track of how far you’ve scrolled, so when you reloaded it would display the initial 50, then jump to wherever you were. The solution is simple, you just need to reset that value once the page is loaded, so that it will overwrite any cached data your browser may be using. The code he posted is as simple as it gets and has been added to the source. You could also fix this by playing with how browser cache your site, but since they usually get the right this seems like a better solution to me.

Copy this code and paste it in your HTML
  1. var loading = false;
  2. $(window).scroll(function(){
  3. if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
  4. if(loading == false){
  5. loading = true;
  6. $('#loadingbar').css("display","block");
  7. $.get("load.php?start="+$('#loaded_max').val(), function(loaded){
  8. $('body').append(loaded);
  9. $('#loaded_max').val(parseInt($('#loaded_max').val())+50);
  10. $('#loadingbar').css("display","none");
  11. loading = false;
  12. });
  13. }
  14. }
  15. });
  17. // Update code, simple add or integrate this.
  18. $(document).ready(function() {
  19. $('#loaded_max').val(50);
  20. });


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.