Jump to content

Colour Swatch Change Image

Recommended Posts

  • Replies 3
  • Views 263
  • Created
  • Last Reply

Top Posters In This Topic

Posted
On 11/22/2024 at 10:26 AM, tuanphan said:

Add this position to change this image? image.thumb.png.cb6a0df8436cd8306bbcc7c6eb3ee1c8.pngYou will have 2 or 3 or 4 colors or?

 

Max 2 options please Tuan! 

Posted

You can follow these

Description: Users click color on right side >> Change Image on left side

image.png.90168bf25cf5c91f0d8a3825a8489f85.png

#1. First, you need to add 4 Image Blocks (we will use 2 Image Blocks for Colors)

image.png.e8556dcadbe4083509b81837e0956fa0.png

#2. Find ID of 4 Image Blocks.
In my example, we will have

  • Blue Image: #block-yui_3_17_2_1_1732782993034_14332
  • Red Image: #block-9bd0a46448e6d6f89699
  • Blue circle: #block-169c54d83e1f15a451a1
  • Red circle: #block-217d3e7c50084a2b2f34

image.png.9038fabb41c84d93fe80d3c6b3315496.png

#3. Use this code to Code Injection > Footer

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    // hide red image on initial
    $('#block-9bd0a46448e6d6f89699').hide();
    // when users click blue circle
    $('#block-169c54d83e1f15a451a1').click(function() {
        $('#block-yui_3_17_2_1_1732782993034_14332').show();
        $('#block-9bd0a46448e6d6f89699').hide();
        $('#block-169c54d83e1f15a451a1').addClass('active');
        $('#block-217d3e7c50084a2b2f34').removeClass('active');
    });
    // when users click red circle
    $('#block-217d3e7c50084a2b2f34').click(function() {
        $('#block-9bd0a46448e6d6f89699').show();
        $('#block-yui_3_17_2_1_1732782993034_14332').hide();
        $('#block-217d3e7c50084a2b2f34').addClass('active');
        $('#block-169c54d83e1f15a451a1').removeClass('active');
    });
});
</script>

image.png.d847db5681bf51d52580d89a1987d90c.png

Remember to replace ID in the code

image.png.4a9a711ec3678df9f7415eb7178e4279.png

#4. To change image opacity when users click red/blue circle image, you can use this code under

<style>
  div.image-block.active img {
   opacity: 0.5;
  }
</style>

image.png.31eaab0ef0b04c0ff72252131311b451.png

Result

image.png.f20a3887780b1b0e2eab3ae446c40de1.png

 

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!)

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.