Jump to content

Lazy Page Loading For Image Sections?

Go to solution Solved by Dada78,

Recommended Posts

Posted

Hey! I know you can do this with summary blocks via this plugin - may be worth asking them if it can be applied to your sections too

Sam Crawford | by Crawford.
 
Multi-award-winning Squarespace expert
 
💸 How I make $500k/year designing Squarespace websites: Watch the video
📱 Hire me to build your Squarespace website: calendly.com/bycrawford/call
 
Did my comment help? To help others benefit, leave a like and mark my answer as 'best'. This lets users scroll straight to the solution they need.
Posted (edited)

I created a quick demo for your case here

https://tuanphan-demo01.squarespace.com/section-load-more-1?noredirect

Pass: abc

(Load more button under Section 3)

image.png.4c0bed83142663e56d22b95a23bbc8de.png

You can use these steps

#1. First, use this code to Page Header Injection

In case Page doesn't Header Injection, you can edit Page > Use a Code Block

<style>
button#show-more {
    display: block;
    text-align: center;
    margin: 0 auto;
    background-color: #000;
    color: #fff;
    padding: 10px 20px;
    border-radius: 50px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
        $(document).ready(function() {
            $('article section.page-section').hide(); 
            $('article section.page-section').slice(0, 3).show(); 
            $('article section.page-section:last-child').after('<button id="show-more">Show More</button>');
            $('#show-more').click(function() {
                $('article .page-section:hidden').slice(0, 3).slideDown(); 

                if ($('article .page-section:hidden').length === 0) {
                    $(this).hide(); 
                }
            });
        });
    </script>

image.thumb.png.7c6928953f9aaedc1e24193abf5210f5.png

#2. Adjust the code

Code will show first 3 sections + hide other sections, when click Load More, it will load the next 3 sections. You can adjust it here

image.thumb.png.a9e48a9c53ff0cb419af3abaa7333485.png

Edited by tuanphan
typo

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Posted

Oh great, thanks so much @tuanphan, will implement it today and report back!

When I inject the code, onto that page is it page specific, or would I need to target a particular page somehow, as I don't need this feature on all pages.

Thank you!

Posted
53 minutes ago, Dada78 said:

Oh great, thanks so much @tuanphan, will implement it today and report back!

When I inject the code, onto that page is it page specific, or would I need to target a particular page somehow, as I don't need this feature on all pages.

Thank you!

If you add code to Code Block or Individual Page Header Injection, the code will run on that page only.

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Posted
8 hours ago, Dada78 said:

How would I be able to implement this lazy loading feature for a masonry image grid section to display on the first 5 images for example and a "load more" button?

Site: https://fish-mayflower-586h.squarespace.com/ PW: testing78

 

Thanks so much!

 

I don't think code will work properly with Masonry

But you can try this code to Homepage Header Injection

<style>
button#show-more {
    display: block;
    text-align: center;
    margin: 0 auto;
    background-color: #000;
    color: #fff;
    padding: 10px 20px;
    border-radius: 50px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
        $(document).ready(function() {
            $('figure.gallery-masonry-item').hide(); 
            $('figure.gallery-masonry-item').slice(0, 5).show(); 
            $('section:has(.gallery-masonry)').after('<button id="show-more">Show More</button>');
            $('#show-more').click(function() {
                $('figure.gallery-masonry-item:hidden').slice(0, 5).slideDown(); 

                if ($('figure.gallery-masonry-item:hidden').length === 0) {
                    $(this).hide(); 
                }
            });
        });
    </script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Posted
14 hours ago, Dada78 said:

Thanks, I used the code, but unfortunately the section doesn't auto-expand to accommodate the additionally loaded images upon click to "load more".

https://fish-mayflower-586h.squarespace.com/
PW: testing78

You can use this new code, it still doesn't work, you can remove both code, I can test easier. 

<style>
button#show-more {
    display: block;
    text-align: center;
    margin: 0 auto;
    background-color: #000;
    color: #fff;
    padding: 10px 20px;
    border-radius: 50px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>

<script>
  $(document).ready(function() {
    $('figure.gallery-masonry-item').hide();
    $('figure.gallery-masonry-item').slice(0, 5).show();
    
    $('section:has(.gallery-masonry)').after('<button id="show-more">Show More</button>');
    
    $('#show-more').click(function() {
        $('figure.gallery-masonry-item:hidden').slice(0, 5).slideDown();

        // autoscroll to refresh page
        $('html, body').animate({ scrollTop: $(window).scrollTop() + 1 }, 0);
        $('html, body').animate({ scrollTop: $(window).scrollTop() - 1 }, 0);

        // 
        if ($('figure.gallery-masonry-item:hidden').length === 0) {
            $(this).hide();
        }
    });
});
</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Posted
3 hours ago, Dada78 said:

Thanks @tuanphan, tried it and unfortunately no luck with this code.

 

I removed it so it might be easier for you to check it out. Thanks so much for looking into it!

New code doesn't do anything, or it runs it creates problem or?

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

  • Solution
Posted (edited)

New code doesn't do anything different. Gallery Wrapper (or section) still doesn't expand to accommodate for subsequently loaded images, but I have figured it out now and am sharing it with anyone who needs it:

 

Inject this into your site's footer:

<script>
document.addEventListener("DOMContentLoaded", function () {
    // Target the gallery wrapper
    const galleryWrapper = document.querySelector('.gallery-masonry-wrapper');
    
    // Check if the gallery wrapper exists
    if (!galleryWrapper) {
        console.error("Gallery wrapper not found.");
        return;
    }

    const loadMoreButton = document.createElement('button');
    const imagesPerBatch = 6;
    let currentImageIndex = 0;

    // Create the "Load More" button with some basic styling
    loadMoreButton.innerHTML = "Load More";
    loadMoreButton.style.display = "block";
    loadMoreButton.style.margin = "20px auto";
    loadMoreButton.style.padding = "10px 20px";
    loadMoreButton.style.cursor = "pointer";
    loadMoreButton.style.backgroundColor = "#000";
    loadMoreButton.style.color = "#fff";
    loadMoreButton.style.border = "none";
    loadMoreButton.style.fontSize = "16px";
    loadMoreButton.style.textAlign = "center";

    // Insert the button after the gallery masonry wrapper
    galleryWrapper.parentElement.appendChild(loadMoreButton);

    // Function to load images in batches of 6
    function loadMoreImages() {
        const allItems = galleryWrapper.querySelectorAll('.gallery-masonry-item');
        
        for (let i = currentImageIndex; i < currentImageIndex + imagesPerBatch && i < allItems.length; i++) {
            const imgElement = allItems[i].querySelector('img');
            
            // Make sure to use the data-src attribute to lazy load images
            if (imgElement && imgElement.getAttribute('data-src')) {
                imgElement.src = imgElement.getAttribute('data-src');
                imgElement.style.display = 'block';  // Show the image if hidden
            }
            
            allItems[i].style.display = 'block';  // Show the corresponding gallery item
        }

        currentImageIndex += imagesPerBatch;

        // Trigger a re-layout for the Masonry grid after images load
        triggerMasonryLayout();

        // Hide the "Load More" button if all images have been loaded
        if (currentImageIndex >= allItems.length) {
            loadMoreButton.style.display = "none";
        }
    }

    // Function to initially hide all images except the first batch
    function hideAllImages() {
        const allItems = galleryWrapper.querySelectorAll('.gallery-masonry-item');
        allItems.forEach((item, index) => {
            if (index >= imagesPerBatch) {
                item.style.display = 'none';
            }
        });
    }

    // Function to trigger the Masonry layout recalculation
    function triggerMasonryLayout() {
        const event = new Event('resize');  // Simulate a window resize
        window.dispatchEvent(event);
    }

    // Initial setup: hide all images except the first batch
    hideAllImages();
    loadMoreImages();  // Load the first batch of images

    // Event listener to load more images on button click
    loadMoreButton.addEventListener('click', loadMoreImages);
});

   
</script>

 

Edited by Dada78
Found my own solution

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.