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
- 虚拟幻灯片模块Keyboard
- 键盘控制模块Mousewheel
- 鼠标滚轮控制模块Navigation
- 导航模块Pagination
- 分页模块Scrollbar
- 滚动条模块Parallax
- 视差模块FreeMode
- 自由模式模块Grid
- 网格模块Manipulation
- 幻灯片操作模块(仅适用于核心版本)Zoom
- 缩放模块Controller
- 控制器模块A11y
- 辅助功能模块History
- 历史导航模块HashNavigation
- 哈希导航模块Autoplay
- 自动播放模块EffectFade
- 淡入淡出效果模块EffectCube
- 立方体效果模块EffectFlip
- 翻转效果模块EffectCoverflow
- 封面流效果模块EffectCards
- 卡片效果模块EffectCreative
- 创意效果模块Thumbs
- 缩略图模块// 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
- A11y 模块所需的样式swiper/css/autoplay
- 自动播放模块所需的样式swiper/css/controller
- 控制器模块所需的样式swiper/css/effect-cards
- 卡片效果模块所需的样式swiper/css/effect-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 wrapper HTML 元素标签 |
onSwiper | (swiper) => void | 'div' | 接收 Swiper 实例的回调 |
它还支持 on{Eventname}
格式的所有Swiper 事件。例如,slideChange
事件变为 onSlideChange
属性
<Swiper
onSlideChange={() => {/*...*/}}
onReachEnd={() => {/*...*/}}
...
>
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
tag | 字符串 | 'div' | Swiper Slide HTML 元素标签 |
zoom | 布尔值 | false | 启用缩放模式所需的额外 wrapper |
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
hook,以便在 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 幻灯片内部组件的另一个 hook,用于获取幻灯片数据(与 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 集成到你的网站或应用程序中非常容易。以下是你的下一步操作