react中swiper插件如何使用?


本文摘自PHP中文网,作者青灯夜游,侵删。

方法:1、使用“npm i swiper -S”命令安装swiper包;2、使用import语句引入swiper的css和js文件;3、在render中编写swiper组件的结构,并在react的挂载生命周期函数内创建Swiper对象即可。

本教程操作环境:windows7系统、react16版,该方法适用于所有品牌电脑。

react中使用基本swiper

第一步:安装包

1

npm i swiper -S

第二步:引包

1

2

import Swiper from 'swiper/dist/js/swiper.js' 

import 'swiper/dist/css/swiper.css'

第三步:写html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!-- Slider main container -->

<p class="swiper-container">

 

    <!-- Additional required wrapper -->

    <p class="swiper-wrapper">

 

        <!-- Slides -->

        <p class="swiper-slide">Slide 1</p>

        <p class="swiper-slide">Slide 2</p>

        <p class="swiper-slide">Slide 3</p>

    </p>

 

    <!-- If we need pagination -->

    <p class="swiper-pagination"></p>

 

    <!-- If we need navigation buttons -->

    <p class="swiper-button-prev"></p>

    <p class="swiper-button-next"></p>

 

    <!-- If we need scrollbar -->

    <p class="swiper-scrollbar"></p>

</p>

第四步:在react声明周期里创建Swiper对象进行调用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// 直接引用数据将,new Swiper放在componentDidMount

// 用axios请求数据,new Swiper放在componentDidUpdate 

 

    new Swiper('.swiper-container', {

        direction: 'vertical',//竖向轮播

        loop: true,//无缝轮播

        pagination: {//小圆点分页

            el: '.swiper-pagination',

        },

        navigation: {//左右分页

            nextEl: '.swiper-button-next',

            prevEl: '.swiper-button-prev',

        },

        scrollbar: {//下划线分页

            el: '.swiper-scrollbar',

        }

    })

更多编程相关知识,请访问:编程入门!!

以上就是react中swiper插件如何使用?的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

React怎么写style

React中如何引入样式

为什么React需要绑定this

使用React native的好处是什么?

React native是什么框架?

什么编辑器支持React

没有babel能运行vue与React

React怎么避免重复点击

React中文什么意思

React中的refs是什么

更多相关阅读请进入《React》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...