I had certain problems by implementing Ajaxinate endless scroll:
The issue was that after clicking on a product and clicking the back button, the scroll position was lost. I tried to implement "Snapback cache", to solve the back button issue, but it didn't work properly:
After clicking the back button in the browser, the position was restored perfectly, but the next loaded pages were starting with page 1 again.
So now I implemented the infinite scroll of infinite-scroll.com, they have a quite good solution to solve the back button problem, and a commercial licence is a one time fee of 25$, which is a fair price to me.
So I show you how I implemented infinite-scroll in my minimal theme to save you some time:
{{ 'infinite-scroll.pkgd.js' | asset_url | script_tag }}
$(document).ready(function() {
$('.infiniteloop').infiniteScroll({
// options
path: '.pagination__next',
append: '.loopload',
status: '.page-load-status',
hideNav: '.pagination',
//scrollThreshold: 1000,
});
});
<div class="infiniteloop"> // <----- Start Div ------>
{% for product in collection.products %}
<li class="grid__item grid__item--{{section.id}} {{ grid_item_width }}">
{% include 'product-card-grid', max_height: max_height %}
</li>
{% else %}
{% comment %}
Add default products to help with onboarding for collections/all only.
The onboarding styles and products are only loaded if the
store has no products.
{% endcomment %}
{% if collection.handle == 'all' and collection.all_vendors.size == 0 and collection.all_types.size == 0 %}
<li class="grid__item">
<div class="grid grid--uniform">
{% for i in (1..limit) %}
<div class="grid__item {{ grid_item_width }}">
<div class="grid-view-item">
<a href="#" class="grid-view-item__link">
<div class="grid-view-item__image">
{% capture current %}{% cycle 1, 2, 3, 4, 5, 6 %}{% endcapture %}
{{ 'product-' | append: current | placeholder_svg_tag: 'placeholder-svg' }}
</div>
<div class="h4 grid-view-item__title">{{ 'homepage.onboarding.product_title' | t }}</div>
<div class="grid-view-item__meta">
<span class="product-price__price">$19.99</span>
</div>
</a>
</div>
</div>
{% endfor %}
</div>
</li>
{% else %}
{%- assign is_empty_collection = true -%}
{% endif %}
{% endfor %}
</div> // <------ End Div ------->
{% if paginate.pages > 1 %}
{% include 'pagination' %}
{% endif %}
<div class="page-load-status">
<div class="infinite-scroll-request loader-ellips">
more products loading...
</div>
<p class="infinite-scroll-last">End of content</p>
<p class="infinite-scroll-error">No more pages to load</p>
</div>
<p class="pagination">
<a class="pagination__next" href="{{ paginate.next.url }}"></a>
</p>
var bottom = this.top + this.element.clientHeight;
var bottom = Math.max( window.innerHeight, document.body.clientHeight )
scrollThreshold: 1000
Like in the demo. It is loaded beginning page 2 after back button. So I put in a title with adding " - page 2" to the collection title. so at least the user knows he's on page 2.
Good solution!
I also can't seem to figure out how to stop infinite scroll from making requests for new pages when there aren't any... the instructions in the demo don't work on my solution could you possibly show how yours works?
Thanks post can you please try this
I only start infinite scroll if I have more than 29 items on my collection page, as I have a pagination of 30 products per page. This is what I put in my theme.js:
$(function () {
if ($('.loopload').length>29){
var $container = $('.ajaxloop').infiniteScroll({
// options
path: '.pagination__next',
append: '.loopload',
status: '.page-load-status',
hideNav: '.pagination',
scrollThreshold: 1400,
});
$('#next-pagination-check').addClass('initialised');
}
});
User | Count |
---|---|
395 | |
204 | |
129 | |
46 | |
42 |