Skip to content

Instantly share code, notes, and snippets.

@atikju
Created November 20, 2018 16:37
Show Gist options
  • Save atikju/ea86cd9863820f0b5849be464b60e086 to your computer and use it in GitHub Desktop.
Save atikju/ea86cd9863820f0b5849be464b60e086 to your computer and use it in GitHub Desktop.
Shopify - Infinite Scrolling on Collection Pages
{% paginate collection.products by 24 %}
<div class="devProductContainer">
{% for product in collection.products %}
{% include 'product-loop' %}
{% endfor %}
</div>
<!--pagination links-->
<div id="pagination"> {{ paginate | default_pagination }} </div>
{% endpaginate %}
<style>
#pagination{ display: none; }
</style>
//This file needs to be hooked at the bottom of theme.liquid right before the </body>
{% if template.name contains 'collection' %}
{% assign product_per_page = 24 %}
<script>
$(document).ready(function() {
var counter = 2
var pages = Math.ceil('{{ collection.all_products_count }}' / {{ product_per_page }});
var maxCount = pages + 1;
console.log("Pages Found: " + pages);
console.log("Collection Title: {{ collection.handle }}");
$('#pagination a').on('click' , function(e) {
e.preventDefault();
var getUrl = "{{shop.url}}/collections/{{ collection.handle }}?page="+counter;
console.log(getUrl);
$.ajax({
url: getUrl,
type:'GET',
// beforeSend: function() {
// showPopup('.loading');
// },
success: function(data){
//hidePopup('.loading');
$(".devProductContainer").append($(data).find('.devProductContainer').html());
counter++;
if(counter == maxCount ) {
$('#more').empty();
}
}
});
});
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$('#pagination a').eq(0).trigger('click');
console.log('clicked');
}
});
});
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment