Skip to content

Instantly share code, notes, and snippets.

@austinsamsel
Forked from rickydazla/collections.liquid.html
Created January 20, 2017 18:01
Show Gist options
  • Save austinsamsel/0c04c5d70fe3d19a68383cdc34637e86 to your computer and use it in GitHub Desktop.
Save austinsamsel/0c04c5d70fe3d19a68383cdc34637e86 to your computer and use it in GitHub Desktop.
"Infinite" scrolling in Shopify collections
{% paginate collection.products by 20 %}
<ul class="collection-matrix">
{% for product in collection.products %}
<li id="product-{{ forloop.index | plus:paginate.current_offset }}">
{% include 'product' with product %}
</li>
{% endfor %}
<li class="top"><a href="#collectionpage">Back to Top</a> &uarr;</li>
{% if paginate.next %}
<li class="more">&darr; <a href="{{ paginate.next.url }}">More</a></li>
{% endif %}
</ul>
<div id="product-list-foot"></div>
{% endpaginate %}
var pInfScrLoading = false;
var pInfScrDelay = 100;
function pInfScrExecute() {
if($(document).height() - 800 < ($(document).scrollTop() + $(window).height())) {
var loadingImage;
pInfScrNode = $('.more').last();
pInfScrURL = $('.more a').last().attr("href");
if(!pInfScrLoading && pInfScrNode.length > 0 && pInfScrNode.css('display') != 'none') {
$.ajax({
type: 'GET',
url: pInfScrURL,
beforeSend: function() {
pInfScrLoading = true;
loadingImage = pInfScrNode.clone().empty().append('<img src=\"http://cdn.shopify.com/s/files/1/0068/2162/assets/loading.gif?105791\" />');
loadingImage.insertAfter(pInfScrNode);
pInfScrNode.hide();
},
success: function(data) {
// remove loading feedback
pInfScrNode.next().remove();
var filteredData = $(data).find(".collection-matrix");
filteredData.insertBefore( $("#product-list-foot") );
loadingImage.remove();
pInfScrLoading = false;
},
dataType: "html"
});
}
}
}
$(document).ready(function () {
$(window).scroll(function(){
$.doTimeout( 'scroll', pInfScrDelay, pInfScrExecute);
if( $(document).height() - 800 > $(document).scrollTop() + $(window).height() ) {
pInfScrDelay = 100;
}
});
});
// https://github.com/cowboy/jquery-dotimeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment