metafizzy / infinite-scroll

📜 Automatically add next page
https://infinite-scroll.com
7.41k stars 1.74k forks source link

Hide Load More button with Scroll 2 pages, then load with button #944

Closed JMKelley closed 3 years ago

JMKelley commented 3 years ago

I'm running the "Load More Button with Scroll 2 Pages" demo perfectly, it's working great. Though I'm having trouble trying to hide my "Load More" button when it reaches it the end of the content. The code I'm running is as follows:

      let $container = $('.article-feed').infiniteScroll({
            path: '.nav-next a',
            append: '.card',
            history: 'push',
            button: '.view-more-button',
            hideNav: '.pagination',
            status: '.page-load-status'
        });

        var isLast = false;

        let $viewMoreButton = $('.view-more-button');

        // get Infinite Scroll instance
        let infScroll = $container.data('infiniteScroll');

        $container.on( 'load.infiniteScroll', onPageLoad );

        function onPageLoad() {
            if ( infScroll.loadCount == 1 ) {
                // after 2nd page loaded
                // disable loading on scroll

                $container.infiniteScroll( 'option', {
                    loadOnScroll: false,
                });
                // show button
                $viewMoreButton.show();
                // remove event listener
                $container.off( 'load.infiniteScroll', onPageLoad );
            }
        }

        $container.on( 'last.infiniteScroll', function( event, response, path ) {
            $viewMoreButton.hide();
            isLast = true;
            console.log( 'Last!' );
        } );

It's reporting the variable Last! fine, though not hiding the button?