Created
July 6, 2015 15:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[!getPage? | |
&elementClass=`modSnippet` | |
&element=`getResources` | |
&parents=`[[!getUrlParam? &name=`parent`]]` | |
&depth=`0` | |
&limit=`3` | |
&pageVarKey=`page` | |
&includeTVs=`1` | |
&includeContent=`1` | |
&hideContainers=`1` | |
&tpl=`item-tpl` | |
]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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