Change background image inside html liked to CMS

Hi guys!

I’m trying to insert a HTML code in a product dynamic page. So far, so good. But I would like to the image inside the HTML code changes according to the corresponding product showing in the dynamic page.

The problem is that I have no idea how to “quote” this CMS product colletion data (mainMedia) inside the HTML.

Any ideas about how could I quote the CMS image inside the field “background-image”?

Here is the code that I’m working on:

<input id="patternSizeSlider" type="range" name="points" min="1" max="20" value="10">

<style data-rapport-style>
</style>

<div class="rapport">
</div>
  
<style>
 .rapport {
  background-color: rgb(252, 246, 245);
  width: 100%;
  height: 500px;
  background-image: url("https://media.discordapp.net/attachments/1180907241294016652/1184719449752875109/Embroidery_Minimalist_Leaves_Preview.png?ex=658cfef7&is=657a89f7&hm=ad7115cf5aea99a7b913a2470877a96074ad0bdd48faeefbb433456fac6dbae2&=&format=webp&quality=lossless&width=671&height=671");
  background-size: var(--size);
}

#patternSizeSlider {
  accent-color: rgb(36, 35, 35);
  display: block;
  margin: 0px auto;
  width: 350px;
  max-width: 90%;
  margin-bottom: 20px;
}</style>

<script>
  const sizeField = document.querySelector("#patternSizeSlider");
const patternConfigTag = document.querySelector("[data-rapport-style]");
const imageArea = document.querySelector(".rapport")

let imageSrc;
let imageSize;

const updateImage = () => {
  patternConfigTag.innerHTML = `
  .rapport {
     --background: url(${imageSrc});
     --size: ${imageSize || 150}px;
  }
  `;
};

sizeField.addEventListener("input", () => {
  imageSize = sizeField.value * 50;
  updateImage();
});

const dropArea = imageArea;

dropArea.addEventListener('dragover', (event) => {
  event.stopPropagation();
  event.preventDefault();
  // Style the drag-and-drop as a "copy file" operation.
  event.dataTransfer.dropEffect = 'copy';
});

dropArea.addEventListener('drop', (event) => {
  event.stopPropagation();
  event.preventDefault();
  const fileList = event.dataTransfer.files;
  const [imageFile] = fileList;
    if (imageFile) {
      imageSrc = URL.createObjectURL(imageFile);
    }
  updateImage();
});
</script>

The image in the left should be the same as the right one.

Thank you in advance!

Show codes always as code-blocks, not as images.

You will have to expand your CODE (HTML-CODE + Wix-Page-CODE).

-You will have to establish a CONNECTION between HTML-Component and your Page-Data.
-You are in the need to send some commands to your html-component → to be able to control the images or what ever inside your HTML-Component.

Generate a ‘COMMAND’ field inside your database, where you will store all commands inside a STRING, ARRAY or even OBJECT-Field (BEST SOLUTION would maybe be the object-option).

Let’s say you have then stored a COMMAND-OBJECT inside your COMMAND-DATABASE-FIELD (for each of your DATAROWS inside your DB), something like…

{
command: ‘copy’, //<----- add, delete, create, select, whatever…
elemenType: ‘div’, //<----- image, dropdown, text whatever…
source: ‘yoursourcecode’, //<----- for example image-sourcecode…
add more informations here for your html-component into this OBJECT.
}

once your object is ready → you send it over with all it’s holding informations over to your html-component. Your HTML-Component must be able to read the recieved OBJECT with all it’s including informations. That means you have to give your HTML-Component some …

The last step would be to get (filter) the right code-object for corresponding product.
Each product would send a DIFFERENT-INFORMATION to the HTML-Component with different set of commands, sources, methods whatever.

Everything would be readed out of your database (including the DATA-OBJECT for HTML-Comp.).

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Interactive Background</title>

  <style data-rapport-style>
  </style>

  <style>
    .rapport {
      background-color: rgb(252, 246, 245);
      width: 100%;
      height: 500px;
      background-image: url("https://www.brainline.org/sites/all/modules/custom/bl_brain/images/brain-lateral.png");
      background-size: var(--size);
    }

    #patternSizeSlider {
      accent-color: rgb(36, 35, 35);
      display: block;
      margin: 0px auto;
      width: 350px;
      max-width: 90%;
      margin-bottom: 20px;
    }
  </style>
</head>

<body>

  <input id="patternSizeSlider" type="range" name="points" min="1" max="20" value="10">

  <div class="rapport">
  </div>

  <script>
    const sizeField = document.querySelector("#patternSizeSlider");
    const patternConfigTag = document.querySelector("[data-rapport-style]");
    const imageArea = document.querySelector(".rapport")

    let imageSrc;
    let imageSize;

    const updateImage = () => {
      patternConfigTag.innerHTML = `
      .rapport {
         --background: url(${imageSrc});
         --size: ${imageSize || 150}px;
      }
      `;
    };

    sizeField.addEventListener("input", () => {
      imageSize = sizeField.value * 50;
      updateImage();
    });

    const dropArea = imageArea;

    dropArea.addEventListener('dragover', (event) => {
      event.stopPropagation();
      event.preventDefault();
      // Style the drag-and-drop as a "copy file" operation.
      event.dataTransfer.dropEffect = 'copy';
    });

    dropArea.addEventListener('drop', (event) => {
      event.stopPropagation();
      event.preventDefault();
      const fileList = event.dataTransfer.files;
      const [imageFile] = fileList;
      if (imageFile) {
        imageSrc = URL.createObjectURL(imageFile);
      }
      updateImage();
    });
  </script>

</body>

</html>

Add the following to your HTML-Component-CODE:

  1. <body onload="init();"> //<---- this will load the → init-function, when component is ready.

  2. your init-function should look like…

function init () {
  // when a message is received from the page code
  window.onmessage = (event) => {
    if (event.data) {
      console.log("HTML Code Element received a message!");
      insertMessage(event.data);
    }
  }
}
  1. Do not forget —> you are sending a simple OBJECT —> {a:aaa, y:yyy, z:444}

  2. Once your HTML-C. has recieved the OBJECT-DATA → generate a code inside the HTML-C. which will know what to do with the recieved data and how to handle it.