Skip to content

Commit 815a539

Browse files
committed
feat: image indicator
1 parent 87e200a commit 815a539

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/components/ImagePopup.astro

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ const images = Object.entries(allImages)
3737
</div>
3838

3939
<div id="img-container" class="mb-4 overflow-hidden aspect-[3/2]">
40-
<img id="popup-img" class="w-full h-full object-cover rounded-lg" alt={`Image from ${title}`} />
40+
<img id="popup-img" class="w-full h-full object-cover rounded-lg transition-opacity duration-200" alt={`Image from ${title}`} />
4141
</div>
4242

43+
<div id="image-indicators" class="flex gap-2 mb-4 justify-center "></div>
44+
4345
<h3 class="font-bold text-lg mb-3">About {title}</h3>
4446

4547
<div id="popup-desc" class="text-base mb-4">
@@ -59,20 +61,47 @@ const images = Object.entries(allImages)
5961
const popupBg = document.getElementById(id);
6062
const closeBtn = popupBg?.querySelector('#close-popup');
6163
const imgElement = popupBg?.querySelector('#popup-img');
64+
const indicatorsContainer = popupBg?.querySelector('#image-indicators');
6265

63-
if (!popupBg || !closeBtn || !imgElement) return;
66+
if (!popupBg || !closeBtn || !imgElement || !indicatorsContainer) return;
6467

6568
let currentImageIndex = 0;
6669

70+
function changeImage(newIndex) {
71+
currentImageIndex = newIndex;
72+
imgElement.style.opacity = '0.5';
73+
setTimeout(() => {
74+
imgElement.src = images[currentImageIndex];
75+
imgElement.style.opacity = '1';
76+
updateIndicators();
77+
}, 100);
78+
}
79+
80+
function updateIndicators() {
81+
indicatorsContainer.innerHTML = '';
82+
images.forEach((imageSrc, index) => {
83+
const indicator = document.createElement('img');
84+
indicator.src = imageSrc;
85+
indicator.className = `w-12 h-8 object-cover rounded cursor-pointer transition-transform hover:scale-110 ${
86+
index === currentImageIndex ? 'ring-2 ring-white' : 'opacity-70'
87+
}`;
88+
indicator.onclick = () => {
89+
changeImage(index);
90+
};
91+
indicatorsContainer.appendChild(indicator);
92+
});
93+
}
94+
6795
function initImages() {
6896
if (images.length > 0) {
6997
imgElement.src = images[0];
98+
updateIndicators();
7099
}
71100

72101
imgElement.onclick = () => {
73102
if (images.length <= 1) return;
74-
currentImageIndex = (currentImageIndex + 1) % images.length;
75-
imgElement.src = images[currentImageIndex];
103+
const nextIndex = (currentImageIndex + 1) % images.length;
104+
changeImage(nextIndex);
76105
};
77106
}
78107

-10.6 MB
Loading
-10.4 MB
Loading

0 commit comments

Comments
 (0)