fix(shop): 修复商品详情页轮播图显示不完整问题
- 将图片裁剪模式从 aspectFill 改为 widthFix,确保图片宽度撑满屏幕 - 新增 swiperHeight 状态,根据首张图片实际宽高比动态计算轮播图高度 - 通过图片 onLoad 事件设置动态高度,避免图片被裁剪不完整 - 优化图片组件布局,移除高度限制,防止图片变形
This commit is contained in:
@@ -25,6 +25,7 @@ const ProductDetailPage: React.FC = () => {
|
||||
const router = useRouter()
|
||||
const id = Number(router.params.id || router.params.goodsId)
|
||||
const [product, setProduct] = useState<ShopGoods | null>(null)
|
||||
const [swiperHeight, setSwiperHeight] = useState(375)
|
||||
const [skuVisible, setSkuVisible] = useState(false)
|
||||
const [skuMode, setSkuMode] = useState<'cart' | 'buy'>('cart')
|
||||
const [isFavorite, setIsFavorite] = useState(false)
|
||||
@@ -230,7 +231,7 @@ const ProductDetailPage: React.FC = () => {
|
||||
{/* 图片轮播 */}
|
||||
<Swiper
|
||||
className='w-full'
|
||||
style={{ height: '375px' }}
|
||||
style={{ height: `${swiperHeight}px` }}
|
||||
indicatorDots
|
||||
indicatorColor='#e5e7eb'
|
||||
indicatorActiveColor='#0e932e'
|
||||
@@ -240,9 +241,20 @@ const ProductDetailPage: React.FC = () => {
|
||||
{images.map((img, idx) => (
|
||||
<SwiperItem key={idx}>
|
||||
<Image
|
||||
className='w-full h-full'
|
||||
className='w-full'
|
||||
style={{ display: 'block' }}
|
||||
src={img}
|
||||
mode='aspectFill'
|
||||
mode='widthFix'
|
||||
onLoad={(e: any) => {
|
||||
// 根据第一张图片的实际宽高比动态设置 Swiper 高度
|
||||
if (idx === 0) {
|
||||
const { width, height } = e.detail
|
||||
if (width > 0) {
|
||||
const screenWidth = Taro.getSystemInfoSync().windowWidth
|
||||
setSwiperHeight(Math.round((height / width) * screenWidth))
|
||||
}
|
||||
}
|
||||
}}
|
||||
onClick={() => Taro.previewImage({ current: img, urls: images })}
|
||||
/>
|
||||
</SwiperItem>
|
||||
|
||||
Reference in New Issue
Block a user