Tuesday, September 28, 2010

10:59 PM
1
If you have used Google Reader, then you might have noticed the way Google Reader shows feed items, it loads up few items first when you click on a feed and as you scroll down to view more items, it fetches more items dynamically and adds it to the list.


                                 

Dzone also implements this technique to show items as you scroll down the list.

                                                      

Similarly, Facebook has also started using this technique to load more posts in user’s home page as you scroll down.



Today, I am going to create a similar dynamic content box that loads more content dynamically as user scrolls to the bottom of the box.

The HTML Structure

<div id="container">
<div id="scrollbox" >
<div id="content" >
<p>Lorem ipsum dolor sit amet</p>
<p>Ipsum lorem dolor amet sit</p>
<p>Dolor lorem ipsum amet tis</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Ipsum lorem dolor amet sit</p>
<p>Dolor lorem ipsum amet tis</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Ipsum lorem dolor amet sit</p>
<p>Dolor lorem ipsum amet tis</p>
</div>
</div>
<p><span id="status" ></span></p>
</div>

The scrollbox div is the one that has fixed width and height and houses the content div that stores the content. The status span is used to display number of loaded items or the status message of loading more items.

The CSS Styles

The height and width of scrolling div is fixed using CSS and its overflow property is set to auto so that it gets a scrollbar when content exceeds the height or width. But to prevent horizontal scrolling, i added overflow-x:hidden attribute so that scrolling only occurs up or down.


#container{
width:400px;
margin:0px auto;
padding:40px 0;
}
#scrollbox{
width:400px;
height:300px;
overflow:auto; overflow-x:hidden;
}
#container > p{
background:#eee;
color:#666;
font-family:Arial, sans-serif; font-size:0.75em;
padding:5px; margin:0;
text-align:right;
}

The Concept

Now to load more content on scroll, you will need to understand the concept behind the JavaScript code given below. To start with, i will be using, three attributes of DOM objects namely, scrollHeight, clientHeight and scrollTopscrollHeight is the actual height of the inner content of a box, i.e. the actual height of content in the scrollbox container in our case.clientHeight specifies the height of the viewport container i.e. height of the visible region which is the height of scrollbox div in our case.
The scrollTop is the distance in pixels the content which has moved up.

Here is an illustraton specifying these properties.
When the scrollTop becomes equal to scrollHeight-clientHeight, then the scroll bar has reached its bottom position, so this is the point when we need to fetch new items. To make the scrolling box more useful, i added another value called scrolloffset, which keeps track of the fact that how many pixels before the bottom the scroll bar is, so that we can start fetching new items instead of the user scrolling to the bottom and then waiting for content to load.

The JavaScript Code

Here’s the JavaScript code you will need. As always, you will need to include the latest jQuery library into your page header before this code.
$('document').ready(function(){
updatestatus();
scrollalert();
});
function updatestatus(){
//Show number of loaded items
var totalItems=$('#content p').length;
$('#status').text('Loaded '+totalItems+' Items');
}
function scrollalert(){
var scrolltop=$('#scrollbox').attr('scrollTop');
var scrollheight=$('#scrollbox').attr('scrollHeight');
var windowheight=$('#scrollbox').attr('clientHeight');
var scrolloffset=20;
if(scrolltop>=(scrollheight-(windowheight+scrolloffset)))
{
//fetch new items
$('#status').text('Loading more items...');
$.get('new-items.html', '', function(newitems){
$('#content').append(newitems);
updatestatus();
});
}
setTimeout('scrollalert();', 1500);
}
Code Explanation: The updatestatus() function counts the number of items loaded into the content and displays it in the status span. The scrollalert function checks the current scrollTop position of the scrollbox div and if the scroll bar is near the bottom area, new items are fetched using AJAX.
Here i am using new-items.html that simply displays some items for the content box as this is just a demo. In actual application, you might employ some server side script that will load next items from the result set.

The content obtained via AJAX is then appended to the content div and updatestatus() is called to update the status message. setTimeout is used so that position of the scroll bar is checked periodically and if it matches condition, then new items should be fetched.
So, there you are with a dynamic content box just like Google Reader or DZone.

1 comments:

Denny Wijaya said...

CoooLLLLL..!!! like dis