Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckchaudhary/8629087 to your computer and use it in GitHub Desktop.
Save ckchaudhary/8629087 to your computer and use it in GitHub Desktop.
/**
* automatically load activities when user scrolls down - like facebook
* @author: @webdwall - http://webdeveloperswall.com/buddypress/load-more-activites-on-scroll
**/
jQuery(window).load(function(){
/*
* 1. bind event on page scroll
* 2. check if user has reched near to the bottom of activity stream( ScrollTop and pageheight etc..)
* 3. load the new content
* 4. update the pageheight etc..
*/
var e_a_al_inAction = false;
/* first we will load new content when user has scrolled down 80% of the total height.
* next time when user has scrolled 85%.. next time 90% and so on till 95%
* Why the extra pain? ... Save server resources :)
*/
var e_a_al_factor = 80;
//80% of the height of the document, offset scroll height to start loading new content
var e_a_al_offset = (jQuery(document).height())*( 0.8 );
jQuery(document).scroll(function(){
if(jQuery(document).scrollTop() > e_a_al_offset && e_a_al_inAction == false){
e_a_al_inAction = true;
jQuery("#content li.load-more").addClass('loading');
if ( null == jQuery.cookie('bp-activity-oldestpage') )
jQuery.cookie('bp-activity-oldestpage', 1, {
path: '/'
} );
var oldest_page = ( jQuery.cookie('bp-activity-oldestpage') * 1 ) + 1;
jQuery.post( ajaxurl, {
action: 'activity_get_older_updates',
'cookie': encodeURIComponent(document.cookie),
'page': oldest_page
},
function(response)
{
jQuery("#content li.load-more").removeClass('loading');
jQuery.cookie( 'bp-activity-oldestpage', oldest_page, {
path: '/'
} );
jQuery("#content li.load-more").replaceWith(response.contents);
//reset and setup everything for next load request
e_a_al_inAction = false;
e_a_al_factor += 5;
if( e_a_al_factor>95 ){ e_a_al_factor=95; }
e_a_al_offset = (jQuery(document).height())*( e_a_al_factor/100 );
}, 'json' );
return false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment