Home > Web Front-end > JS Tutorial > body text

How to use the swiper plug-in in react?

青灯夜游
Release: 2020-11-30 14:01:13
Original
3553 people have browsed it

Method: 1. Use the "npm i swiper -S" command to install the swiper package; 2. Use the import statement to introduce swiper's css and js files; 3. Write the structure of the swiper component in render and add it in react Just create the Swiper object within the mounting life cycle function.

How to use the swiper plug-in in react?

The operating environment of this tutorial: windows7 system, react16 version. This method is suitable for all brands of computers.

Using basic swiper in react

Step 1: Install the package

npm i swiper -S
Copy after login

Step 2: Introduce the package

import Swiper from 'swiper/dist/js/swiper.js'  
import 'swiper/dist/css/swiper.css'
Copy after login

Step 3: Write html

    <!-- 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>
Copy after login

Step 4: Create Swiper in the react declaration cycle Object is called

// 直接引用数据将,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',
        }
    })
Copy after login

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of How to use the swiper plug-in in react?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template