Infinite scroll / endless scrolling with infinite-scroll.com

I had certain problems by implementing Ajaxinate endless scroll:

Ajaxinate infinite 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:

Insert Snapback cache

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:

  1. download infinite-scroll.pkgd.js and upload it in shopify assets

  2. in theme-liquid, directly above , I implemented:

{{ 'infinite-scroll.pkgd.js' | asset_url | script_tag }}
  1. in theme.js right at the end, insert following:
$(document).ready(function() {
    $('.infiniteloop').infiniteScroll({
  // options
  path: '.pagination__next',
  append: '.loopload',
  status: '.page-load-status',
  hideNav: '.pagination',
  //scrollThreshold: 1000,
});
});
  1. -Open your sections folder and open collection-template.liquid
    -Find your for loop. You need to wrap a div with the class of “infiniteloop” before the forloop begins and after the forloop ends:
  // <----- Start Div ------>
        {% for product in collection.products %}
          - {% include 'product-card-grid', max_height: max_height %}
          

        {% 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 %}
            - {% for i in (1..limit) %}
                  

                    

                      
                        

                            {% capture current %}{% cycle 1, 2, 3, 4, 5, 6 %}{% endcapture %}
                            {{ 'product-' | append: current | placeholder_svg_tag: 'placeholder-svg' }}
                        

                          {{ 'homepage.onboarding.product_title' | t }}

                        
                            $19.99
                        

                      
                    

                  

                  {% endfor %}
              

            
          {% else %}
            {%- assign is_empty_collection = true -%}
          {% endif %}
        {% endfor %}
        
 // <------ End Div -------> ​
  1. Then find your pagination at the end of collection-template.liquid, by default is says this:
{% if paginate.pages > 1 %}
      {% include 'pagination' %}
    {% endif %}​

Replace that with this:


  

    more products loading...
  

  

End of content

  

No more pages to load

    

  1. Check if it’s working (on collection pages). In my case, the infinite scroll was firing as soon as i started scrolling, so i checked in the js file why the offsets of the scroll position was calculated wrong. I found it and solved it like this:
    I changed infinite-scroll.pkgd.js in line 1368
var bottom = this.top + this.element.clientHeight;

with following

var bottom = Math.max( window.innerHeight, document.body.clientHeight )

Then it was firing a bit late, so I implemented

scrollThreshold: 1000

in theme.js (by removing the “//”)

I forgot one thing, I added the class “loopload” in the for- loop, just the line below the for loop starts in the collection-template.liquid, so the line looks like this:


This seems like the standard install for infinte-scroll.com

Is the back button/save position working? Did you have to update/add any additional options to make that work?

Thanks for your post. I’m using minimal theme as well. Just to be sure, I can start with going to infinite-scroll.com or I need to add some codes before that? Thanks. Al

Thanks for this! but how did you solve the retain-position-in-infinite scroll problem? When I hit back on browser, i get loaded from page 2 without content from page 1

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?

@JakobZabala

Thanks post can you please try this

https://www.huratips.com/tech-tips/how-to-add-infinite-scroll-pagination-to-shopify-collection-pages.html

Hi,

I have tried this method with no luck, infinite scroll works for me, except for my one problem

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');
  }
});

I still keep getting all these page requests when scrolling to the bottom even trying your method

what is the problem for the user with the page requests? Is it visible at the end of the scroll list that new requests are sent?

In my collection-template.liquid, I have this code


  {% if paginate.pages > 1 %}
    {% if paginate.next %}
      Nächste Seite
      

        

          

            
            
            
            
          

        

        

        

Seitenende

      

    {% endif %}
  {% endif %}

Just received a message, v4 for infinite-scroll.com was just released. Did it change anything in the behaviour with your issue?

I will see if the new Infinite Scroll solves this. Luckily there is no noticeable visual problem for the user… but I don’t like the idea of the site potentially loading loads more data unnecessarily. Thanks!

I have a problem with my scroll- it freezes on some images and won’t move to the next image. When I get frustrated and try to swipe or click back the page goes black and blank and I cannot go back to my website at all. Is this the solution to that?