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

Swiper滑块网格展示顺序错误求助(React 18+Swiper 10)

修复Swiper网格滑块条目顺序问题

你的问题出在Grid模块的填充逻辑配置上:当前设置grid.fill: 'row'结合slidesPerView:1、rows:2时,Swiper会按列优先的规则填充滑块——先填满第一列的所有行,再处理下一列,这就导致了条目顺序错乱。

要实现每页上下排列2个条目、按1→2→3→4→5的顺序分页展示,只需调整Grid的fill属性:

  • 将grid.fill的值改为'column'
  • 保留rows:2和slidesPerView:1的配置(每页对应1列2行,刚好容纳2个条目)

修改后的完整代码:

<Swiper
  ref={swiperRef}
  spaceBetween={0}
  allowTouchMove={true}
  touchMove={true}
  slidesPerView={1}
  grid={{
    fill: 'column',
    rows: 2,
  }}
  pagination={{
    type: 'fraction'
  }}
  className="mySwiper"
  modules={[Grid, Pagination]}
  onInit={(swiper) => {
    setPagination(swiper?.pagination?.el?.outerText)
  }}
  onSlideChange={onChangeSlider}
>
  {results?.map((item, index) => (
    <SwiperSlide key={index}>
      <div className="bg-[#e2e2e2] py-4 my-2 text-[#010101]">{index + 1}</div>
    </SwiperSlide>
  ))}
</Swiper>

调整后,Swiper会按行优先的逻辑填充每页内容:第一页显示1、2,第二页3、4,第三页显示5,完全匹配你的预期效果。

补充说明:如果移除grid.fill,Swiper默认会用'column'填充,但此时slidesPerView:1会让所有滑块排成一列连续展示,不会触发分页,因此需要显式配置fill: 'column'来保证分页逻辑正常工作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 14:55:24