Jump to content

Change Header Image For Every Few Pixels The Mouse Moves

Recommended Posts

Hi everyone, I am looking into injecting a custom script into the homepage of my website (jamie-smith.com). Specifically, I would like to have the header image on the home page cycle through the images in a folder in my asset library for every, say 10 pixels the users mouse moves over that section. I tried to implement this basic script cycling between just two images (the original and one other) to see what would happen, but to no avail. Does anyone have an idea on how to implement this?

 

Quote

 

<script>

        document.addEventListener('DOMContentLoaded', function() {

            const section = document.querySelector('[data-section-id="6584502d29d1c614e5bb2213"]');

            const originalImage = 'https://images.squarespace-cdn.com/content/v1/658450003ddc98294ae845f2/f1d532b3-30d8-487d-9fa3-ef5bf82d4776/26.jpg';

            const newImage = 'https://images.squarespace-cdn.com/content/v1/658450003ddc98294ae845f2/f31aab4f-37ed-4dcf-8b94-030660df9c32/ok+here.jpg';

 

            let lastMouseX = 0;

 

            section.addEventListener('mousemove', (event) => {

                const deltaX = Math.abs(event.clientX - lastMouseX);

 

                // Change the background image every 5 pixels moved

                if (deltaX >= 5) {

                    lastMouseX = event.clientX;

 

                    // Toggle between the original and new image

                    section.style.backgroundImage = section.style.backgroundImage === `url("${originalImage}")` ? `url("${newImage}")` : `url("${originalImage}")`;

                }

            });

        });

    </script>

 

 

Link to comment
  • Replies 0
  • Views 1.1k
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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.