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

如何在Swiper JS中缩小幻灯片尺寸且避免末尾留白?

Swiper产品卡片轮播优化方案

问题背景

使用Swiper JS开发产品卡片轮播组件时,遇到以下问题:

  • 幻灯片间空白过多,卡片间距不符合"最小间距"的需求
  • 手动设置swiper-slide宽度后,部分产品卡片无法正常显示,且轮播末尾会出现额外空白

解决步骤

1. 调整Swiper核心配置参数

修改<swiper-container>的属性,让布局自适应卡片宽度:

  • 将slides-per-view设为'auto':Swiper会自动根据卡片宽度计算当前可显示的卡片数量,避免固定数量导致的空白
  • 减小space-between值(比如设为10):直接控制卡片间的最小间距
  • 移除free-mode="true":自由滑动模式容易导致末尾空白,改用默认的锁定滑动模式,确保轮播对齐

2. 修正容器与卡片的样式

  • 移除.swiper-container的height:100%设置,改为自适应内容高度,避免上下空白:
    .swiper-container {
      width: 100%;
      height: auto;
      padding: 20px 0; /* 可选:给轮播上下添加内边距,避免卡片贴边 */
    }
    
  • 不要直接修改swiper-slide的宽度,让它自适应内部product-card的宽度,确保所有卡片都能被正确渲染

3. 修改后的完整代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Swiper with Product Cards</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />

  <style>
    html,
    body {
      position: relative;
      height: 100%;
    }

    body {
      background: #eee;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color: #000;
      margin: 0;
      padding: 0;
    }

    .swiper-container {
      width: 100%;
      height: auto;
      padding: 20px 0;
    }

    .swiper-slide {
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .product-card {
      width: 150px;
      height: 239px;
      background-color: #fff;
      padding: 10px;
      box-sizing: border-box;
      border: 1px solid #ccc;
      border-radius: 5px;
    }

    .product-card img {
      max-width: 100%;
      height: auto;
    }
  </style>
</head>

<body>
  <swiper-container class="mySwiper" pagination="true" pagination-clickable="true" slides-per-view="auto"
    space-between="10">
    <swiper-slide>
      <div class="product-card">
        <img src="product1.jpg" alt="Product 1">
        <h3>Product 1</h3>
        <p>Description of Product 1</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product2.jpg" alt="Product 2">
        <h3>Product 2</h3>
        <p>Description of Product 2</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product3.jpg" alt="Product 3">
        <h3>Product 3</h3>
        <p>Description of Product 3</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product4.jpg" alt="Product 4">
        <h3>Product 4</h3>
        <p>Description of Product 4</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product5.jpg" alt="Product 5">
        <h3>Product 5</h3>
        <p>Description of Product 5</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product6.jpg" alt="Product 6">
        <h3>Product 6</h3>
        <p>Description of Product 6</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product7.jpg" alt="Product 7">
        <h3>Product 7</h3>
        <p>Description of Product 7</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product8.jpg" alt="Product 8">
        <h3>Product 8</h3>
        <p>Description of Product 8</p>
      </div>
    </swiper-slide>
    <swiper-slide>
      <div class="product-card">
        <img src="product9.jpg" alt="Product 9">
        <h3>Product 9</h3>
        <p>Description of Product 9</p>
      </div>
    </swiper-slide>
  </swiper-container>

  <script src="https://cdn.jsdelivr.net/npm/swiper@9/swiper-element-bundle.min.js"></script>
</body>

</html>

额外说明

如果需要保留自由滑动模式,可重新添加free-mode="true",但需确保slides-per-view="auto"和合适的space-between值,同时可以设置centeredSlides="false"来避免居中导致的空白。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 20:24:56