美化产品详情页主图导航图标

This commit is contained in:
jingrow 2025-08-24 20:44:06 +08:00
parent f161eda997
commit 01f3c85526

View File

@ -35,6 +35,58 @@ const ProductImageGallery = ({ data }) => {
const mainImageIndex = getMainImageIndex(); const mainImageIndex = getMainImageIndex();
const currentImage = data.attachments[currentImageIndex]?.file_url || data.attachments[0]?.file_url; const currentImage = data.attachments[currentImageIndex]?.file_url || data.attachments[0]?.file_url;
//
const scrollThumbnailToIndex = (index) => {
console.log('滚动缩略图到索引:', index); //
if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) {
const swiper = thumbsSwiperRef.current.swiper;
console.log('Swiper实例存在开始滚动'); //
// 使
try {
swiper.slideTo(index, 300);
console.log('滚动成功,目标索引:', index);
} catch (error) {
console.error('滚动失败:', error);
}
} else {
console.log('Swiper实例不存在thumbsSwiperRef:', thumbsSwiperRef.current);
}
};
const changeMainImage = (direction) => {
console.log('changeMainImage 被调用,方向:', direction); //
if (data.attachments.length <= 1) return;
let newIndex;
if (direction === 'next') {
newIndex = (currentImageIndex + 1) % data.attachments.length;
} else {
newIndex = (currentImageIndex - 1 + data.attachments.length) % data.attachments.length;
}
console.log('新的索引:', newIndex, '当前索引:', currentImageIndex); //
setCurrentImageIndex(newIndex);
//
setTimeout(() => {
console.log('开始执行滚动,索引:', newIndex); //
scrollThumbnailToIndex(newIndex);
}, 100);
};
const changeMainImageByIndex = (index) => {
setCurrentImageIndex(index);
//
setTimeout(() => {
scrollThumbnailToIndex(index);
}, 100);
};
// //
useEffect(() => { useEffect(() => {
setCurrentImageIndex(mainImageIndex); setCurrentImageIndex(mainImageIndex);
@ -57,10 +109,12 @@ const ProductImageGallery = ({ data }) => {
case 'Home': case 'Home':
event.preventDefault(); event.preventDefault();
setCurrentImageIndex(0); setCurrentImageIndex(0);
setTimeout(() => scrollThumbnailToIndex(0), 100);
break; break;
case 'End': case 'End':
event.preventDefault(); event.preventDefault();
setCurrentImageIndex(data.attachments.length - 1); setCurrentImageIndex(data.attachments.length - 1);
setTimeout(() => scrollThumbnailToIndex(data.attachments.length - 1), 100);
break; break;
default: default:
break; break;
@ -75,61 +129,7 @@ const ProductImageGallery = ({ data }) => {
}; };
}, [data.attachments.length]); }, [data.attachments.length]);
const changeMainImage = (direction) => {
if (data.attachments.length <= 1) return;
if (direction === 'next') {
setCurrentImageIndex((prev) => (prev + 1) % data.attachments.length);
} else {
setCurrentImageIndex((prev) => (prev - 1 + data.attachments.length) % data.attachments.length);
}
};
const changeMainImageByIndex = (index) => {
setCurrentImageIndex(index);
//
if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) {
const swiper = thumbsSwiperRef.current.swiper;
const slideIndex = index;
//
const slideWidth = 64; // 64px
const spaceBetween = 16; // 16px
const totalSlideWidth = slideWidth + spaceBetween;
// slides
const visibleSlides = swiper.params.slidesPerView;
//
const currentTranslate = swiper.translate;
const currentSlideIndex = Math.round(Math.abs(currentTranslate) / totalSlideWidth);
//
let targetSlideIndex;
if (slideIndex < currentSlideIndex) {
//
targetSlideIndex = Math.max(0, slideIndex - 1);
} else if (slideIndex >= currentSlideIndex + visibleSlides) {
//
targetSlideIndex = slideIndex - visibleSlides + 1;
} else {
//
return;
}
//
const targetTranslate = targetSlideIndex * totalSlideWidth;
//
const maxTranslate = swiper.maxTranslate();
const finalTranslate = Math.max(0, Math.min(targetTranslate, maxTranslate));
//
swiper.slideTo(Math.round(finalTranslate / totalSlideWidth), 300);
}
};
// - // -
// const goToPrevThumbs = () => { // const goToPrevThumbs = () => {