Site logo

Archived topic

Adding a "loading" spinner to infinite scroll

8 replies · Started by madmanweb on June 13, 2019

Viewing posts 1–9 of 9

Hi.

How can I add a "loading" spinner GIF to the infinite scroll feature? Sometimes my scroll is smooth and seamless but sometimes it seems to take forever and I don't want people to think they've reached the end of the content.

That option adds a "Load more" button instead of constantly loading posts while scrolling down. So it's not what I'm looking for.

Hi there,

First, you would need to add your loading icon to the site.

1. Create a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the Hook to after_main_content
3. Set the priority to 100
4. Add this content:

<div class="infinite-loading" style="display: none;">
    Loading...
</div>

Then you need to add javascript to show the icon as things load:

jQuery( document ).ready( function($) {
    var $container = $( '#main article' ).first().parent();

    $container.on( 'request.infiniteScroll', function( event, response, path, items ) {
        $( '.infinite-loading' ).show();
    } );

    $container.on( 'append.infiniteScroll', function( event, response, path, items ) {
        $( '.infinite-loading' ).hide();
    } );
} );

If you don't have a place to add javascript, you can:

1. Create a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook to wp_footer
3. Add the above javascript within <script> tags like this:

<script>
    // Javascript above
</script>

Let me know if you need more info :)

Hmm, so this partially works, Tom. The problem is that the "Loading" DIV appears only after the first batch of posts. After you've scrolled to the bottom of the next X posts, it doesn't appear any more. I've checked this using the Chrome dev tools in the code. Only one instance of "Loading" found.

Is this currently added to your website? I'm not seeing the loading div at all.

Let me know :)

Hmm, can you try the after_primary_content_area instead?

This archived topic is closed to new replies.