当前位置:   article > 正文

react 中使用swiper_react swiper

react swiper

本文是官网翻译:Swiper React Components

配置项含义可参考​​​​​​​

Swiper中文网-轮播图幻灯片js插件,H5页面前端开发

安装

Swiper React 只能够通过 NPM 安装,作为Swiper 主库的一部分:

npm i swiper

用法

swiper/react 暴露了 2 个组件: Swiper 和 SwiperSlide:

  1. // Import Swiper React components
  2. import { Swiper, SwiperSlide } from 'swiper/react';
  3. // Import Swiper styles
  4. import 'swiper/swiper.scss';
  5. export default () => {
  6. return (
  7. <Swiper
  8. spaceBetween={50}
  9. slidesPerView={3}
  10. initialSlide={1} // 初始化显示哪一个
  11. loop={true} // 是否循环
  12. onSlideChange={() => console.log('slide change')}
  13. onSwiper={(swiper) => console.log(swiper)}
  14. threshold={40}
  15. >
  16. <SwiperSlide>Slide 1</SwiperSlide>
  17. <SwiperSlide>Slide 2</SwiperSlide>
  18. <SwiperSlide>Slide 3</SwiperSlide>
  19. <SwiperSlide>Slide 4</SwiperSlide>
  20. ...
  21. </Swiper>
  22. );
  23. };

默认情况下,Swiper React使用Swiper的核心版本(没有任何附加组件)。如果要使用导航、分页和其他组件,必须先安装它们。

可以引入的模块:

各模块的作用可参考 中文api - Swiper中文网

  • Virtual - Virtual Slides module
  • Keyboard - Keyboard Control module
  • Mousewheel - Mousewheel Control module
  • Navigation - Navigation module
  • Pagination - Pagination module
  • Scrollbar - Scrollbar module
  • Parallax - Parallax module
  • Zoom - Zoom module
  • Lazy - Lazy module
  • Controller - Controller module
  • A11y - Accessibility module
  • History - History Navigation module
  • HashNavigation - Hash Navigation module
  • Autoplay - Autoplay module
  • EffectFade - Fade Effect module
  • EffectCube - Cube Effect module
  • EffectFlip - Flip Effect module
  • EffectCoverflow - Coverflow Effect module
  • Thumbs - Thumbs module
  1. // 引入 Swiper core 和必须的组件
  2. import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
  3. import { Swiper, SwiperSlide } from 'swiper/react';
  4. // 引入 Swiper 样式
  5. import 'swiper/swiper.scss';
  6. import 'swiper/components/navigation/navigation.scss';
  7. import 'swiper/components/pagination/pagination.scss';
  8. import 'swiper/components/scrollbar/scrollbar.scss';
  9. // 安装 Swiper 组件
  10. SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);
  11. export default () => {
  12. return (
  13. <Swiper
  14. spaceBetween={50}
  15. slidesPerView={3}
  16. navigation
  17. pagination={{ clickable: true }}
  18. scrollbar={{ draggable: true }}
  19. onSwiper={(swiper) => console.log(swiper)}
  20. onSlideChange={() => console.log('slide change')}
  21. >
  22. <SwiperSlide>Slide 1</SwiperSlide>
  23. <SwiperSlide>Slide 2</SwiperSlide>
  24. <SwiperSlide>Slide 3</SwiperSlide>
  25. <SwiperSlide>Slide 4</SwiperSlide>
  26. ...
  27. </Swiper>
  28. );
  29. };

注意, 如果你没有传递相关的参数(例如 没有 navigation.nextElpagination.el, 等.) ,Swiper React 组件会为Navigation、Pagination 和 Scrollbar创建必要的元素。

Styles

Swiper 包包含不同的  CSS, Less 和 SCSS 样式:

CSS 样式

CSS 样式用于 bundle 版:

  • swiper-bundle.css - 包含全部样式,包括模块样式  (如 Navigation, Pagination, 等.)
  • swiper-bundle.min.css - 跟上面的相同,不过是压缩过后的min版

Less 样式

Less 样式分为核心部分和组件部分:

  • swiper.less - 核心 Swiper 样式

 下面是模块样式:

  • components/a11y/a11y.less - A11y 组件样式
  • components/controller/controller.less - styles required for Controller component
  • components/effect-coverflow/effect-coverflow.less - styles required for Coveflow Effect component
  • components/effect-cube/effect-cube.less - styles required for Cube Effect component
  • components/effect-fade/effect-fade.less - styles required for Fade Effect component
  • components/effect-flip/effect-flip.less - styles required for Flip Effect component
  • components/lazy/lazy.less - styles required for Lazy component
  • components/navigation/navigation.less - styles required for Navigation component
  • components/pagination/pagination.less - styles required for Pagination component
  • components/scrollbar/scrollbar.less - styles required for Scrollbar component
  • components/thumbs/thumbs.less - styles required for Thumbs component
  • components/zoom/zoom.less - styles required for Zoom component

SCSS 样式

SCSS 样式分为核心部分和组件部分:

  • swiper.scss - 核心 Swiper 样式

下面是模块样式

  • components/a11y/a11y.scss - styles required for A11y component
  • components/controller/controller.scss - styles required for Controller component
  • components/effect-coverflow/effect-coverflow.scss - styles required for Coveflow Effect component
  • components/effect-cube/effect-cube.scss - styles required for Cube Effect component
  • components/effect-fade/effect-fade.scss - styles required for Fade Effect component
  • components/effect-flip/effect-flip.scss - styles required for Flip Effect component
  • components/lazy/lazy.scss - styles required for Lazy component
  • components/navigation/navigation.scss - styles required for Navigation component
  • components/pagination/pagination.scss - styles required for Pagination component
  • components/scrollbar/scrollbar.scss - styles required for Scrollbar component
  • components/thumbs/thumbs.scss - styles required for Thumbs component
  • components/zoom/zoom.scss - styles required for Zoom component

Swiper 属性

Swiper 组件接收所有 Swiper 参数 作为组件属性, 同时增加了一些额外属性:

PropTypeDefaultDescription
tagstring'div'Swiper 容器, 是一个HTML 元素标签
wrapperTagstring'div'Swiper wrapper HTML 元素标签
onSwiper(swiper) => void一个接收 Swiper实例的回调

他也接收所有的 Swiper 事件, 采用on{Eventname} 形式. 例如 slideChange 事件变成 onSlideChange 属性:

  1. <Swiper
  2. onSlideChange={() => {/*...*/}}
  3. onReachEnd={() => {/*...*/}}
  4. ...
  5. >

SwiperSlide 属性

PropTypeDefaultDescription
tagstring'div'Swiper Slide HTML元素标签
zoombooleanfalse为zoom 模式开启额外的wrapper

SwiperSlide render 函数

SwiperSlide 组件可以接受render 函数,该函数会接收到一个对象,包含如下属性:

  • isActive - 当前slide 是否active
  • isPrev - 是否active slide 的前一个slide
  • isNext - 是否active slide 的前后一个slide
  • isVisible - 当前 slide 是否可见 (watchSlidesVisibility Swiper 参数必须开启)
  • isDuplicate - 当前 slide 是否是一个复制slide (当loop 模式开启的时候)

例如:

  1. <Swiper>
  2. <SwiperSlide>
  3. {({isActive}) => (
  4. <div>Current slide is {isActive ? 'active' : 'not active'}</div>
  5. )}
  6. </SwiperSlide>
  7. </Swiper>

Slots(占位符)

Swiper 使用占位符控制内容分布. 有4个可用的占位符

  • container-start - 元素会被添加到 swiper-container前面
  • container-end (default) - e元素会被添加到 swiper-container后面
  • wrapper-start - 元素会被添加到swiper-wrapper前面
  • wrapper-end -元素会被添加到swiper-wrapper后面

例如:

  1. <Swiper>
  2. <SwiperSlide>Slide 1</SwiperSlide>
  3. <SwiperSlide>Slide 2</SwiperSlide>
  4. <span slot="container-start">Container Start</span>
  5. <span slot="container-end">Container End</span>
  6. <span slot="wrapper-start">Wrapper Start</span>
  7. <span slot="wrapper-end">Wrapper End</span>
  8. </Swiper>

会渲染成:

  1. <div class="swiper-container">
  2. <span slot="container-start">Container Start</span>
  3. <div class="swiper-wrapper">
  4. <span slot="wrapper-start">Wrapper Start</span>
  5. <div class="swiper-slide">Slide 1</div>
  6. <div class="swiper-slide">Slide 2</div>
  7. <span slot="wrapper-end">Wrapper End</span>
  8. </div>
  9. <span slot="container-end">Container End</span>
  10. </div>

虚拟滚动

你只需设置 virtual:true  ,其他的由react进行处理

  1. import SwiperCore, { Virtual } from 'swiper';
  2. import { Swiper, SwiperSlide } from 'swiper/react';
  3. // 安装 Virtual 模块
  4. SwiperCore.use([Virtual]);
  5. export default () => {
  6. // Create array with 1000 slides
  7. const slides = Array.from({ length: 1000 }).map((el, index) => `Slide ${index + 1}`);
  8. return (
  9. <Swiper spaceBetween={50} slidesPerView={3} virtual>
  10. {slides.map((slideContent) => {
  11. <SwiperSlide key={slideContent}>{slideContent}</SwiperSlide>;
  12. })}
  13. </Swiper>
  14. );
  15. };

Controller

Controller 需要传递一个Swiper 实例到另一个:

  1. import React, { useState } from 'react';
  2. import SwiperCore, { Controller } from 'swiper';
  3. import { Swiper, SwiperSlide } from 'swiper/react';
  4. // 安装 Swiper Controller 组件
  5. Swiper.use([Controller]);
  6. const App = () => {
  7. // 保存 controlled swiper 实例
  8. const [controlledSwiper, setControlledSwiper] = useState(null);
  9. return (
  10. <main>
  11. {/* Main Swiper -> 传入 controlled swiper 实例 */}
  12. <Swiper controller={{ control: controlledSwiper }} ...>
  13. {/* ... */}
  14. </Swiper>
  15. {/* Controlled Swiper -> 保存 swiper 实例 */}
  16. <Swiper onSwiper={setControlledSwiper} ...>
  17. {/* ... */}
  18. </Swiper>
  19. </main>
  20. )
  21. }

   

 双向控制 (两个Swipers 相互控制) 它应该像这样:

  1. import React, { useState } from 'react';
  2. import SwiperCore, { Controller } from 'swiper';
  3. import { Swiper, SwiperSlide } from 'swiper/react';
  4. // 安装 Swiper's Controller 组件
  5. Swiper.use([Controller]);
  6. const App = () => {
  7. // 保存 swiper 实例
  8. const [firstSwiper, setFirstSwiper] = useState(null);
  9. const [secondSwiper, setSecondSwiper] = useState(null);
  10. return (
  11. <main>
  12. <Swiper onSwiper={setFirstSwiper} controller={{ control: secondSwiper }}>
  13. {/* ... */}
  14. </Swiper>
  15. <Swiper onSwiper={setSecondSwiper} controller={{ control: firstSwiper }}>
  16. {/* ... */}
  17. </Swiper>
  18. </main>
  19. )
  20. }

缩略图

和 controller 一样,我们需要保存 thumbs 实例,并将他传递给main gallery:

  1. import React, { useState } from 'react';
  2. import SwiperCore, { Thumbs } from 'swiper';
  3. import { Swiper, SwiperSlide } from 'swiper/react';
  4. // install Swiper's Thumbs component
  5. Swiper.use([Thumbs]);
  6. const App = () => {
  7. // store thumbs swiper instance
  8. const [thumbsSwiper, setThumbsSwiper] = useState(null);
  9. return (
  10. <main>
  11. {/* Main Swiper -> pass thumbs swiper instance */}
  12. <Swiper thumbs={{ swiper: thumbsSwiper }} ...>
  13. {/* ... */}
  14. </Swiper>
  15. {/* Thumbs Swiper -> store swiper instance */}
  16. <Swiper onSwiper={setThumbsSwiper} ...>
  17. {/* ... */}
  18. </Swiper>
  19. </main>
  20. )
  21. }

动效

有下面动态效果:

  • Fade
  • Cube
  • Overflow
  • Flip

安装并引入 (Fade 的例子):

  1. import SwiperCore, {EffectFade} from 'swiper';
  2. SwiperCore.use([EffectFade]);

你可以运行这个 例子.

淡入淡出动效例子

  1. import React from 'react';
  2. import { Swiper, SwiperSlide } from 'swiper/react';
  3. import SwiperCore, {EffectFade} from 'swiper';
  4. import 'swiper/swiper.scss';
  5. import 'swiper/components/effect-fade/effect-fade.scss';
  6. SwiperCore.use([EffectFade]);
  7. export default () => {
  8. return (
  9. <Swiper effect="fade">
  10. {[1, 2, 3].map((i, el) => {
  11. return <SwiperSlide>Slide {el}</SwiperSlide>;
  12. })}
  13. </Swiper>
  14. );
  15. };

快速开始视频

要获得更全面的实践教程,请看这段15分钟的入门视频

https://youtu.be/l4kFO6VQPfA

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号