A little how-to, mostly for myself, so I don’t forget!
When you want to load more content on a page, but don’t want the user to have to click a link — implementing something called “infinite scrolling” is a good option. It seems there is quite a debate — see comments in the article — on whether or not this is a good practice. Personally, I like it, but some people seem to find pagination more useful. I think it depends on the application.
Here’s how to implement it:
Add a div in your page where you’d like to add more content when the users scrolls.
Get the latest version of the jquery library or alternatively just use the jquery library hosted by Google and add this piece of code at the bottom of your page (right before the tag):
<javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"/>
Then, add this code:
$(document).ready(function(){
var height = $(document).height() - $(window).height() - 30;
var scrollTriggered = false;
$(window).scroll(function(){
if($(window).scrollTop() >= height && !scrollTriggered )
{
scrollTriggered = true;
$("#pageContent").append("
LOADING MORE CONTENT HERE
");
scrollTriggered = false;
}
});
});
It’s a good practice to add javascript code at the bottom of your page. The developers at Yahoo have a list of good practices to speed up your website, one of them being adding javascript at the bottom.
Here’s a little rundown of how the code works:
- First calculate the height of the visible area of your page and subtract 30. Subtracting 30 makes it so the content is loaded before the user reaches the absolute bottom of the page.
var height = $(document).height() - $(window).height() - 30;
- Add the scroll event handler (I’ll explain the scrollTriggered boolean in the next step). This code allows you to do something when your page is scrolled.
$(window).scroll(function(){}) - Then, check if the position of the scroll bar is more than the calculated height (see step 1) and the scroll event hasn’t already been triggered.
if($(window).scrollTop() >= height && !scrollTriggered )
This is where the scrollTriggered boolean comes into play. When the scrollTriggered boolean is true, even if the scroll event is triggered multiple times, nothing will happen until the scrollTriggered boolean gets set back to false. This is important if you are making an AJAX request and need to retrieve a certain data set. If you don’t have this boolean, you could possibly retrieve the same data set multiple times.
- Set scrollTriggered = true and then either append some content (as in the above example) or make an AJAX request to retrieve more data.
scrollTriggered = true; $("#pageContent").append("LOADING MORE CONTENT HERE
"); scrollTriggered = false;
OR
scrollTriggered = true; $.post('ajax/loadmore',function(data){scrollTriggered = false; $("#pageContent").append(data);})In the AJAX example above, notice I set the scrollTriggered back to false in the callback function. If you do make an AJAX request, make sure you set the scrollTriggered boolean back to false in the callback function, not after the entire AJAX request.
That’s about it! Not too complicated. And useful too!
