Swiper React 仅作为主 Swiper 库的一部分通过 NPM 提供
npm i swiper
swiper/react
导出了 2 个组件:Swiper
和 SwiperSlide
// Import Swiper React components
import { Swiper, SwiperSlide } from 'swiper/react';
// Import Swiper styles
import 'swiper/css';
export default () => {
return (
<Swiper
spaceBetween={50}
slidesPerView={3}
onSlideChange={() => console.log('slide change')}
onSwiper={(swiper) => console.log(swiper)}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
...
</Swiper>
);
};
以下是来自 swiper/modules
的其他模块导入列表
Virtual
- 虚拟幻灯片模块键盘
- 键盘控制模块鼠标滚轮
- 鼠标滚轮控制模块导航
- 导航模块分页
- 分页模块滚动条
- 滚动条模块视差
- 视差模块自由模式
- 自由模式模块网格
- 网格模块操作
- 幻灯片操作模块(仅适用于核心版本)缩放
- 缩放模块控制器
- 控制器模块无障碍
- 无障碍模块历史记录
- 历史记录导航模块哈希导航
- 哈希导航模块自动播放
- 自动播放模块淡入淡出效果
- 淡入淡出效果模块立方体效果
- 立方体效果模块翻转效果
- 翻转效果模块Coverflow 效果
- Coverflow 效果模块卡片效果
- 卡片效果模块创意效果
- 创意效果模块缩略图
- 缩略图模块// import Swiper core and required modules
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/react';
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
export default () => {
return (
<Swiper
// install Swiper modules
modules={[Navigation, Pagination, Scrollbar, A11y]}
spaceBetween={50}
slidesPerView={3}
navigation
pagination={{ clickable: true }}
scrollbar={{ draggable: true }}
onSwiper={(swiper) => console.log(swiper)}
onSlideChange={() => console.log('slide change')}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
...
</Swiper>
);
};
navigation.nextEl
、pagination.el
等),Swiper React 组件将为导航、分页和滚动条创建所需的元素。Swiper 包含不同的 CSS、Less 和 SCSS 样式集
swiper/css
- 仅核心 Swiper 样式swiper/css/bundle
- 所有 Swiper 样式,包括所有模块样式(如导航、分页等)模块样式(如果你已经导入了捆绑样式,则不需要)
swiper/css/a11y
- 无障碍模块所需的样式swiper/css/autoplay
- 自动播放模块所需的样式swiper/css/controller
- 控制器模块所需的样式swiper/css/effect-cards
- 卡片效果模块所需的样式swiper/css/effect-coverflow
- Coverflow 效果模块所需的样式swiper/css/effect-creative
- 创意效果模块所需的样式swiper/css/effect-cube
- 立方体效果模块所需的样式swiper/css/effect-fade
- 淡入淡出效果模块所需的样式swiper/css/effect-flip
- 翻转效果模块所需的样式swiper/css/free-mode
- 自由模式模块所需的样式swiper/css/grid
- 网格模块所需的样式swiper/css/hash-navigation
- 散列导航模块所需的样式swiper/css/history
- 历史记录模块所需的样式swiper/css/keyboard
- 键盘模块所需的样式swiper/css/manipulation
- 操作模块所需的样式swiper/css/mousewheel
- 鼠标滚轮模块所需的样式swiper/css/navigation
- 导航模块所需的样式swiper/css/pagination
- 分页模块所需的样式swiper/css/parallax
- 视差模块所需的样式swiper/css/scrollbar
- 滚动条模块所需的样式swiper/css/thumbs
- 缩略图模块所需的样式swiper/css/virtual
- 虚拟模块所需的样式swiper/css/zoom
- 缩放模块所需的样式对于 Less 样式,请在导入路径中将 css
替换为 less
,例如
import 'swiper/less';
import 'swiper/less/navigation';
import 'swiper/less/pagination';
对于 SCSS 样式,请在导入路径中将 css
替换为 scss
,例如
import 'swiper/scss';
import 'swiper/scss/navigation';
import 'swiper/scss/pagination';
Swiper
React 组件接收所有 Swiper 参数 作为组件属性,以及一些额外的属性
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
tag | 字符串 | 'div' | Swiper 容器 HTML 元素标签 |
wrapperTag | 字符串 | 'div' | Swiper 包装 HTML 元素标签 |
onSwiper | (swiper) => void | 'div' | 接收 Swiper 实例的回调 |
它还支持所有 Swiper 事件,格式为 on{Eventname}
。例如,slideChange
事件变为 onSlideChange
属性
<Swiper
onSlideChange={() => {/*...*/}}
onReachEnd={() => {/*...*/}}
...
>
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
tag | 字符串 | 'div' | Swiper 幻灯片 HTML 元素标签 |
zoom | 布尔值 | false | 启用缩放模式所需的附加包装 |
virtualIndex | 数字 | 实际的 swiper 幻灯片索引。需要为虚拟幻灯片设置 |
SwiperSlide
组件可以接受一个渲染函数,该函数接收一个包含以下属性的对象
isActive
- 当前幻灯片处于活动状态时为 trueisPrev
- 当前幻灯片是活动幻灯片的前一个幻灯片时为 trueisNext
- 当前幻灯片是活动幻灯片的下一个幻灯片时为 trueisVisible
- 当前幻灯片可见时为 true(必须启用 watchSlidesProgress
Swiper 参数)isDuplicate
- 当前幻灯片是重复幻灯片时为 true(当启用 loop
模式时)。例如<Swiper>
<SwiperSlide>
{({ isActive }) => (
<div>Current slide is {isActive ? 'active' : 'not active'}</div>
)}
</SwiperSlide>
</Swiper>
Swiper React 提供 useSwiper
钩子,以便轻松获取 Swiper 中组件内的 Swiper 实例
// some-inner-component.jsx
import { React } from 'react';
import { useSwiper } from 'swiper/react';
export default function SlideNextButton() {
const swiper = useSwiper();
return (
<button onClick={() => swiper.slideNext()}>Slide to the next slide</button>
);
}
useSwiperSlide
是 Swiper 幻灯片内组件的另一个钩子,用于获取幻灯片数据(与 SwiperSlide 渲染函数 中的数据相同)
// some-inner-component.jsx
import { React } from 'react';
import { useSwiperSlide } from 'swiper/react';
export default function SlideTitle() {
const swiperSlide = useSwiperSlide();
return (
<p>Current slide is {swiperSlide.isActive ? 'active' : 'not active'}</p>
);
}
Swiper React 使用“插槽”进行内容分发。有 4 个可用的插槽
container-start
- 元素将添加到 swiper-container 的开头container-end
(默认) - 元素将添加到 swiper-container 的末尾wrapper-start
- 元素将添加到 swiper-wrapper 的开头wrapper-end
- 元素将添加到 swiper-wrapper 的末尾例如
<Swiper>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<span slot="container-start">Container Start</span>
<span slot="container-end">Container End</span>
<span slot="wrapper-start">Wrapper Start</span>
<span slot="wrapper-end">Wrapper End</span>
</Swiper>
将渲染为
<div class="swiper">
<span slot="container-start">Container Start</span>
<div class="swiper-wrapper">
<span slot="wrapper-start">Wrapper Start</span>
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<span slot="wrapper-end">Wrapper End</span>
</div>
<span slot="container-end">Container End</span>
</div>
此处虚拟幻灯片的渲染完全由 React 处理,除了设置 virtual:true
属性和在幻灯片上设置 virtualIndex
之外,不需要任何其他操作
import { Virtual } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/react';
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/virtual';
export default () => {
// Create array with 1000 slides
const slides = Array.from({ length: 1000 }).map(
(el, index) => `Slide ${index + 1}`
);
return (
<Swiper modules={[Virtual]} spaceBetween={50} slidesPerView={3} virtual>
{slides.map((slideContent, index) => (
<SwiperSlide key={slideContent} virtualIndex={index}>
{slideContent}
</SwiperSlide>
))}
</Swiper>
);
};
控制器需要将一个 Swiper 实例传递给另一个实例
import React, { useState } from 'react';
import { Controller } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/react';
const App = () => {
// store controlled swiper instance
const [controlledSwiper, setControlledSwiper] = useState(null);
return (
<main>
{/* Main Swiper -> pass controlled swiper instance */}
<Swiper modules={[Controller]} controller={{ control: controlledSwiper }} ...>
{/* ... */}
</Swiper>
{/* Controlled Swiper -> store swiper instance */}
<Swiper modules={[Controller]} onSwiper={setControlledSwiper} ...>
{/* ... */}
</Swiper>
</main>
)
}
对于双向控制(当两个 Swiper 相互控制时),它应该如下所示
import React, { useState } from 'react';
import { Controller } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/react';
const App = () => {
// store swiper instances
const [firstSwiper, setFirstSwiper] = useState(null);
const [secondSwiper, setSecondSwiper] = useState(null);
return (
<main>
<Swiper
modules={[Controller]}
onSwiper={setFirstSwiper}
controller={{ control: secondSwiper }}
>
{/* ... */}
</Swiper>
<Swiper
modules={[Controller]}
onSwiper={setSecondSwiper}
controller={{ control: firstSwiper }}
>
{/* ... */}
</Swiper>
</main>
);
};
与控制器相同,我们需要存储缩略图实例并将其传递给主图库
import React, { useState } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import { Thumbs } from 'swiper/modules';
const App = () => {
// store thumbs swiper instance
const [thumbsSwiper, setThumbsSwiper] = useState(null);
return (
<main>
{/* Main Swiper -> pass thumbs swiper instance */}
<Swiper modules={[Thumbs]} thumbs={{ swiper: thumbsSwiper }} ...>
{/* ... */}
</Swiper>
{/* Thumbs Swiper -> store swiper instance */}
{/* It is also required to set watchSlidesProgress prop */ }
<Swiper
modules={[Thumbs]}
watchSlidesProgress
onSwiper={setThumbsSwiper}
...
>
{/* ... */}
</Swiper>
</main>
)
}
以下效果可用
要使用效果,您必须先导入并安装它们(与所有其他模块一样)。
您可以在此处找到正在运行的 效果演示。
import React from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import { EffectFade } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/effect-fade';
export default () => {
return (
<Swiper modules={[EffectFade]} effect="fade">
{[1, 2, 3].map((i, el) => {
return <SwiperSlide>Slide {el}</SwiperSlide>;
})}
</Swiper>
);
};
Create React App 还不支持纯 ESM 包。仍然可以使用 Swiper(7.2.0+)配合使用。
在这种情况下,我们需要使用直接文件导入
// Core modules imports are same as usual
import { Navigation, Pagination } from 'swiper/modules';
// Direct React component imports
import { Swiper, SwiperSlide } from 'swiper/swiper-react.mjs';
// Styles must use direct files imports
import 'swiper/swiper.scss'; // core Swiper
import 'swiper/modules/navigation.scss'; // Navigation module
import 'swiper/modules/pagination.scss'; // Pagination module
正如您所见,将 Swiper 集成到您的网站或应用中非常容易。因此,以下是您的下一步