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!