I’m using WooCommerce with the following theme: CUBE NATURE Pro – Bikys
On the product detail page, the default product gallery only allows switching product images by clicking the thumbnail images below the main image.
I want to enhance the user experience by adding left and right arrow buttons to the sides of the main images, so that visitors can navigate through the gallery more easily.
I’ve implemented the feature using the code below:
<style>
.woo-gallery-nav {position: absolute;top: 50%;z-index: 10;transform: translateY(-50%);background: rgba(0,0,0,0.5);color: #ffdb00;font-size: 30px;width: 30px;height: 30px;line-height: 30px;text-align: center;cursor: pointer;user-select: none;border-radius: 15px;transition: all 0.3s ease;}
.woo-gallery-nav:hover {background: rgba(0,0,0,0.8);}
.woo-gallery-nav.prev {left: 0px;}
.woo-gallery-nav.next {right: 0px;}
.flex-control-thumbs li {cursor: pointer;transition: opacity 0.3s ease;}
.flex-control-thumbs li.flex-active {opacity: 0.7;border: 2px solid #333;}
.woocommerce-product-gallery__image {transition: opacity 0.5s ease;}
</style>
<script>
document.addEventListener("DOMContentLoaded", function () {
function initializeGalleryNavigation() {
const galleryWrapper = document.querySelector(".woocommerce-product-gallery__wrapper");
const slides = Array.from(galleryWrapper.querySelectorAll('.woocommerce-product-gallery__image'));
const thumbs = Array.from(document.querySelectorAll(".flex-control-thumbs li"));
if (!slides.length || !thumbs.length) {
setTimeout(initializeGalleryNavigation, 100);
return;
}
let currentIndex = slides.findIndex(slide => slide.classList.contains('flex-active-slide'));
if (currentIndex === -1) currentIndex = 0;
const prevBtn = document.createElement("div");
prevBtn.className = "woo-gallery-nav prev";
prevBtn.innerHTML = "‹";
const nextBtn = document.createElement("div");
nextBtn.className = "woo-gallery-nav next";
nextBtn.innerHTML = "›";
galleryWrapper.parentNode.appendChild(prevBtn);
galleryWrapper.parentNode.appendChild(nextBtn);
function updateGallery(index) {
if (index < 0) index = slides.length - 1;
if (index >= slides.length) index = 0;
thumbs[index].querySelector('img').click();
currentIndex = index;
}
prevBtn.addEventListener("click", () => updateGallery(currentIndex - 1));
nextBtn.addEventListener("click", () => updateGallery(currentIndex + 1));
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowLeft") updateGallery(currentIndex - 1);
if (e.key === "ArrowRight") updateGallery(currentIndex + 1);
});
thumbs.forEach((thumb, i) => {
thumb.addEventListener("click", () => {
currentIndex = i;
});
});
}
initializeGalleryNavigation();
});
</script>
and it’s somewhat functional. However, I’ve encountered a small bug on mobile devices: When using the arrow buttons to switch images, the logic sometimes conflicts with the default thumbnail click behavior. For example, clicking the arrows may occasionally cause the thumbnails to stop working, or clicking a thumbnail may cause the arrow buttons to stop functioning correctly.
Is this an issue with how I’m handling the event bindings or gallery state? Do I need to further optimize my JavaScript to handle mobile interactions more gracefully?
Thanks in advance!