如何在react中引入swiper滑动插件

如何在react中引入swiper

一、使用npm安装swiper包

npm i swiper -S

二、使用import引入swiper包

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

三、编写swiper的HTML代码

    <!-- Slider main container -->
    <div class="swiper-container">
        <!-- Additional required wrapper -->
        <div class="swiper-wrapper">
            <!-- Slides -->
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
        </div>
        <!-- If we need pagination -->
        <div class="swiper-pagination"></div>
        <!-- If we need navigation buttons -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        <!-- If we need scrollbar -->
        <div class="swiper-scrollbar"></div>
    </div>

四、在react声明周期里创建Swiper对象

// 直接引用数据将,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相关技术文章,请访问React答疑栏目进行学习!

以上就是如何在react中引入swiper滑动插件的详细内容,更多请关注易知道|edz.cc其它相关文章!

推荐阅读