0

0

Swiper.js教程:实现多张幻灯片分组滑动

心靈之曲

心靈之曲

发布时间:2025-11-17 12:26:25

|

416人浏览过

|

来源于php中文网

原创

Swiper.js教程:实现多张幻灯片分组滑动

本教程详细指导如何在swiper.js中配置幻灯片分组滑动功能。通过利用`slidespergroup`参数,开发者可以轻松实现每次点击导航按钮时,同时移动多张幻灯片,而非逐一滑动。这对于展示多列内容(如产品列表或图片画廊)的轮播图尤其有用,能显著提升用户体验和内容展示效率。

引言:Swiper.js中的分组滑动需求

Swiper.js是一款功能强大且广泛使用的现代化触摸滑动组件库,适用于构建各种类型的轮播图、画廊和滑块。在许多场景下,我们可能需要在一个视图中同时展示多张幻灯片(例如,一个产品轮播每次显示三件商品)。默认情况下,Swiper的导航按钮(或通过滑动操作)每次只会移动一张幻灯片。然而,为了提供更直观、更高效的用户体验,尤其是在展示多列内容时,我们通常希望每次点击导航按钮时能够“翻页”式地移动一组幻灯片,而不是逐个移动。本文将深入探讨如何通过Swiper.js的配置参数实现这一需求。

核心配置:slidesPerGroup 参数详解

要实现每次点击导航按钮时移动多张幻灯片,Swiper.js提供了slidesPerGroup参数。

  • slidesPerGroup的作用: 此参数定义了每次滑动(无论是通过导航按钮点击、键盘导航还是触摸滑动)时,Swiper将移动的幻灯片数量。
  • 与slidesPerView的关系: slidesPerGroup通常与slidesPerView参数结合使用。slidesPerView决定了在当前视图中同时显示的幻灯片数量,而slidesPerGroup则控制每次导航操作移动的幻灯片数量。为了实现“翻页”效果,通常会将slidesPerGroup的值设置为与slidesPerView相同。例如,如果slidesPerView: 3且slidesPerGroup: 3,那么每次点击“下一页”按钮,Swiper将移动三张幻灯片,显示下一组三张全新的幻灯片。如果slidesPerGroup: 1而slidesPerView: 3,则每次点击只会移动一张幻灯片,导致视图中的内容部分更新,这可能不是期望的行为。

实现步骤与示例代码

下面我们将通过一个具体的例子来演示如何在Swiper.js中配置slidesPerGroup。

1. 引入Swiper库

首先,在HTML文件中引入Swiper的CSS和JavaScript文件。你可以使用CDN链接,如下所示:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>

2. 构建HTML结构

Swiper需要一个特定的HTML结构:一个主容器(.swiper),一个包裹所有幻灯片的容器(.swiper-wrapper),以及每个单独的幻灯片(.swiper-slide)。此外,为了导航,我们还需要添加前进和后退按钮。

<div class="swiper">
  <div class="home-wrapper-categories swiper-wrapper">
    <!-- 幻灯片内容,此处以图片为例 -->
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
    <article class="product-block swiper-slide">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
    <article class="product-block swiper-slide drop-shadow">
      <header class="product-img">
        <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Placeholder Image">
      </header>
    </article>
  </div>

  <!-- 导航按钮 -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>
</div>

3. 编写CSS样式

为幻灯片和容器添加一些基本的样式,以确保它们能够正确显示和布局。

MusicAI
MusicAI

AI音乐生成工具

下载
.product-block {
  background: #ffffff;
  border-radius: 4px;
}

.product-img img {
  max-width: 100%;
  height: auto;
  aspect-ratio: 4 / 3.5; /* 保持图片比例 */
}

4. 初始化Swiper实例并配置slidesPerGroup

在JavaScript中初始化Swiper,并在配置对象中添加或修改slidesPerGroup参数。为了实现响应式设计,我们通常会在breakpoints中为不同的屏幕宽度设置不同的slidesPerView和slidesPerGroup。

const swiper = new Swiper('.swiper', {
  // 基本参数
  direction: 'horizontal', // 水平滑动
  loop: false,             // 不循环(可根据需求设置)

  // 导航箭头配置
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // 响应式断点配置
  breakpoints: {
    // 当屏幕宽度大于等于600px时
    600: {
      slidesPerView: 3,      // 同时显示3张幻灯片
      slidesPerGroup: 3,     // 每次滑动3张幻灯片
      spaceBetween: 15       // 幻灯片之间间距15px
    }
  }
});

在上述代码中,关键在于breakpoints配置下的slidesPerGroup: 3。它指示Swiper在屏幕宽度达到600px时,不仅显示3张幻灯片(slidesPerView: 3),而且每次点击导航按钮时,也会向前或向后移动3张幻灯片。

完整代码示例

将上述HTML、CSS和JavaScript代码整合在一起,即可得到一个功能完整的Swiper实例,实现每次点击滑动多张幻灯片的效果。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Swiper.js 分组滑动教程</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f0f2f5;
        }
        .swiper {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px 0;
            position: relative;
        }
        .swiper-wrapper {
            display: flex;
        }
        .swiper-slide {
            flex-shrink: 0;
            width: 100%;
            box-sizing: border-box;
        }
        .product-block {
            background: #ffffff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            overflow: hidden;
            text-align: center;
        }
        .product-img img {
            max-width: 100%;
            height: auto;
            aspect-ratio: 4 / 3.5;
            object-fit: cover;
            display: block;
            border-radius: 8px 8px 0 0;
        }
        .swiper-button-prev,
        .swiper-button-next {
            color: #007aff;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 50%;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
            transition: background-color 0.3s ease;
        }
        .swiper-button-prev:hover,
        .swiper-button-next:hover {
            background-color: rgba(255, 255, 255, 1);
        }
        .swiper-button-prev::after,
        .swiper-button-next::after {
            font-size: 20px;
        }
    </style>
</head>
<body>

    <div class="swiper">
        <div class="home-wrapper-categories swiper-wrapper">
            <article class="product-block swiper-slide">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 1">
                </header>
            </article>
            <article class="product-block swiper-slide">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 2">
                </header>
            </article>
            <article class="product-block swiper-slide drop-shadow">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 3">
                </header>
            </article>
            <article class="product-block swiper-slide drop-shadow">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 4">
                </header>
            </article>
            <article class="product-block swiper-slide drop-shadow">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 5">
                </header>
            </article>
            <article class="product-block swiper-slide drop-shadow">
                <header class="product-img">
                    <img src="https://images.unsplash.com/photo-1578985545062-69928b1d9587?ixlib=rb-1.2.1&w=1080&fit=max&q=80&fm=jpg&crop=entropy&cs=tinysrgb" alt="Product 6">
                </header>
            </article>
        </div>

        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>

    <script>
        const swiper = new Swiper('.swiper', {
            direction: 'horizontal',
            loop: false,

            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },

            breakpoints: {
                600: {
                    slidesPerView: 3,
                    slidesPerGroup: 3,
                    spaceBetween: 15
                }
            }
        });
    </script>

</body>
</html>

注意事项与最佳实践

  • slidesPerView与slidesPerGroup的匹配: 最佳实践是将slidesPerGroup设置为与slidesPerView相同的值,这样可以确保每次滑动都能呈现一个完整的新“页面”内容,提供最流畅和可预测的用户体验。
  • 响应式设计: 充分利用breakpoints功能,为不同的设备屏幕尺寸配置不同的slidesPerView和slidesPerGroup。例如,在移动设备上可能只显示一张幻灯片并每次滑动一张,而在桌面端则显示三张并每次滑动三张。
  • 循环模式(loop)的考量: 如果启用了loop: true,请确保slidesPerGroup的值能够整除总幻灯片数量。否则,在循环的边界处可能会出现不规则或不完整的滑动,影响用户体验。如果不能整除,可以考虑禁用loop或调整幻灯片数量。
  • 性能: 尽管Swiper经过高度优化,但在处理大量幻灯片时,设置过大的slidesPerGroup可能会在某些低端设备上影响动画的流畅性。建议在实际项目中进行测试以确保性能达标。
  • 拖动行为: slidesPerGroup不仅影响导航按钮,也影响用户通过拖动或手势滑动时的行为。当用户拖动时,Swiper会尝试对齐到slidesPerGroup定义的组边界。

总结

通过灵活运用slidesPerGroup参数,开发者可以轻松地定制Swiper.js的滑动行为,实现每次点击导航时移动多张幻灯片的功能。这对于构建产品展示、图片画廊或任何需要分组展示内容的轮播图来说至关重要,能够显著提升用户界面的交互性和内容的展示效率。掌握这一参数,将使你在使用Swiper.js时拥有更大的控制力和创造力。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
js正则表达式
js正则表达式

php中文网为大家提供各种js正则表达式语法大全以及各种js正则表达式使用的方法,还有更多js正则表达式的相关文章、相关下载、相关课程,供大家免费下载体验。

531

2023.06.20

js获取当前时间
js获取当前时间

JS全称JavaScript,是一种具有函数优先的轻量级,解释型或即时编译型的编程语言;它是一种属于网络的高级脚本语言,主要用于Web,常用来为网页添加各式各样的动态功能。js怎么获取当前时间呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

576

2023.07.28

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

js是什么意思
js是什么意思

JS是JavaScript的缩写,它是一种广泛应用于网页开发的脚本语言。JavaScript是一种解释性的、基于对象和事件驱动的编程语言,通常用于为网页增加交互性和动态性。它可以在网页上实现复杂的功能和效果,如表单验证、页面元素操作、动画效果、数据交互等。

6283

2023.08.17

js删除节点的方法
js删除节点的方法

js删除节点的方法有:1、removeChild()方法,用于从父节点中移除指定的子节点,它需要两个参数,第一个参数是要删除的子节点,第二个参数是父节点;2、parentNode.removeChild()方法,可以直接通过父节点调用来删除子节点;3、remove()方法,可以直接删除节点,而无需指定父节点;4、innerHTML属性,用于删除节点的内容。

494

2023.09.01

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

Js中concat和push的区别
Js中concat和push的区别

Js中concat和push的区别:1、concat用于将两个或多个数组合并成一个新数组,并返回这个新数组,而push用于向数组的末尾添加一个或多个元素,并返回修改后的数组的新长度;2、concat不会修改原始数组,是创建新的数组,而push会修改原数组,将新元素添加到原数组的末尾等等。本专题为大家提供concat和push相关的文章、下载、课程内容,供大家免费下载体验。

240

2023.09.14

js截取字符串的方法介绍
js截取字符串的方法介绍

JavaScript字符串截取方法,包括substring、slice、substr、charAt和split方法。这些方法可以根据具体需求,灵活地截取字符串的不同部分。在实际开发中,根据具体情况选择合适的方法进行字符串截取,能够提高代码的效率和可读性 。

303

2023.09.21

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

49

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.9万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.6万人学习

CSS教程
CSS教程

共754课时 | 43.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号