
t.classList.toggle('selected', i === idx));
// read correct data-attribute
const newSrc = thumbnails[idx].getAttribute('data-image-large-src');
mainImage.src = newSrc;
currentIndex = idx;
}
// 3) left arrow click
arrowLeft?.addEventListener('click', () => {
const prev = (currentIndex - 1 + thumbnails.length) % thumbnails.length;
updateMainImage(prev);
});
// 4) right arrow click
arrowRight?.addEventListener('click', () => {
const next = (currentIndex + 1) % thumbnails.length;
updateMainImage(next);
});
// 5) Left/Right key on the keyboard
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
const prevKey = (currentIndex === 0)
? thumbnails.length - 1
: currentIndex - 1;
updateMainImage(prevKey);
} else if (event.key === 'ArrowRight') {
const nextKey = (currentIndex === thumbnails.length - 1)
? 0
: currentIndex + 1;
updateMainImage(nextKey);
}
});
// 6) initialize first image
updateMainImage(0);
})();