You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目使用Owl Carousel构建商品画廊时缩略图重复问题求助

问题根源与修复方案

嘿,我一眼就发现问题所在了!你在缩略图画廊的foreach循环里犯了一个很容易忽略的小错误——你始终在引用主图的$product->image路径,而没有使用循环中当前迭代的额外图片$image变量。

错误代码片段(问题核心)

@foreach (json_decode($product->images, true) as $image)
<button class="owl-thumb-item">
  <img src="{{ productImage($product->image) }}" alt="product">
</button>
@endforeach

这里不管循环到哪一张额外图片,你都在调用productImage($product->image)(主图路径),所以缩略图自然会重复显示主图。

修正后的完整代码

<div class="product-images col-lg-6">
  <div data-slider-id="1" class="owl-carousel items-slider owl-drag">
    <div class="item">
      <img src="{{ productImage($product->image) }}" alt="product" id="currentImage">
    </div>
  </div>
  <div data-slider-id="1" class="owl-thumbs d-flex align-items-center justify-content-center">
    <button class="owl-thumb-item">
      <img src="{{ productImage($product->image) }}" alt="product">
    </button>
    @if ($product->images)
      @foreach (json_decode($product->images, true) as $image)
        <button class="owl-thumb-item">
          <img src="{{ productImage($image) }}" alt="product">
        </button>
      @endforeach
    @endif
  </div>
</div>

只需要把foreach循环里的productImage($product->image)改成productImage($image),循环中的每一张额外图片就会加载对应的路径了。

关于你的切换脚本

你的点击切换逻辑是没问题的,只要图片路径正确,点击缩略图就能正常切换主图显示的内容。

内容的提问来源于stack exchange,提问作者user5487286

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:30