Skip to content

Instantly share code, notes, and snippets.

@nick2687
Created July 6, 2015 15:22
Show Gist options
  • Save nick2687/3b5d83bd6474b9e4eb39 to your computer and use it in GitHub Desktop.
Save nick2687/3b5d83bd6474b9e4eb39 to your computer and use it in GitHub Desktop.
SHows how to set up infinate scroll on a site using getPage and getResources
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&parents=`[[!getUrlParam? &name=`parent`]]`
&depth=`0`
&limit=`3`
&pageVarKey=`page`
&includeTVs=`1`
&includeContent=`1`
&hideContainers=`1`
&tpl=`item-tpl`
]]
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&hideContainers=`1`
&limit=`5`
&includeTVs=`1`
&includeContent=`1`
&tpl=`item-tpl`
]]
</section>
<div id="ajaxloader" style="display:none;"><center><img src="assets/img/ajax-loader.gif" width="40px" heigh="40px"/></center></div>
<script>
$(document).ready(function(){
var currentPage = 1; // We start at page 1, of course.
connectorUrl = '[[~127]]?page=PAGEHERE&parent=[[*id]]'; // A generated URL to the CategoryAjax resource
itemsHolder = $('#items'); // The dom element that will contain the items
run = true;
function lastAddedLiveFunc(){
$('div#ajaxloader').show();
run = false;
$.get(connectorUrl.replace('PAGEHERE', currentPage + 1), function(data){
if ($.trim(data) == '') { // result empty? Hide ajax loader and prevent additional calls from being made.
$('div#ajaxloader').hide();
run = false;
}
else { // Increment page ID, append markup to the holder and hide spinner
$('div#ajaxloader').hide();
currentPage++;
itemsHolder.append(data);
run = true;
}
});
};
// Detect window scroll and attempt to run lastAddedLiveFunc
$(window).scroll(function(){
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop/(docheight-winheight)) > scrolltrigger) {
if (run == true) {
lastAddedLiveFunc();
}
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment