I have been trying to get this working but can't. I am using Astro (Tailwind and React, but not relevant here). I want to have one main image, that is replaced when you click on one of the smaller images that are mapped out from my collections data.
<div class="hidden gap-x-3 gap-y-5 px-3 md:grid md:grid-cols-3 md:px-5">
<div
class="min-h-96 aspect-w-1 aspect-h-1 lg:aspect-none col-span-3 w-full overflow-hidden rounded-md bg-gray-100 group-hover:opacity-75 lg:h-96"
>
<div class="h-96 min-w-full overflow-hidden rounded-lg bg-slate-50">
<Image
src={featureImage}
alt={fiAlt}
width="800"
height="800"
class="h-96 w-full object-cover"
format="webp"
quality="low"
transition:name={`image-${slug}`}
id="imagebox"
/>
</div>
</div>
{
images.map((image) => (
<button
id={image}
class="imagechange h-50 aspect-w-1 aspect-h-1 col-span-1 w-full overflow-hidden rounded-md bg-gray-100 shadow-lg group-hover:opacity-75"
>
<Image
src={image}
alt={fiAlt}
width="800"
height="800"
class="h-full w-full object-cover"
format="webp"
quality="low"
/>
</button>
))
}
</div>
I tried this Javascript, but it did not work at all - Astro apparently cannot generate the right src from the collections data because I am using astro-assets I presume.
<script is:inline>
window.addEventListener('DOMContentLoaded', (event) => {
const img = document.getElementById("imagebox");
const buttons = document.querySelectorAll('button.imagechange');
buttons.forEach(button => {
button.addEventListener('click', function() {
img.src = button.querySelector('img').src;
img.alt = button.querySelector('img').alt;
});
});
});
</script>
Can anyone point me to the right direction?