react 使用swiper插件

这篇博客介绍了如何在React应用中使用Swiper库创建轮播效果。通过引入Swiper组件,设置slidesPerView和pagination属性,实现了滑动切换和分页指示器功能。同时,根据屏幕尺寸动态计算每页展示的数据数量,确保了在不同设备上的良好适配。文章还展示了如何监听滑动事件以更新导航按钮的状态。
摘要由CSDN通过智能技术生成

首先引入插件 npm i swiper

如果需要做适配,可以通过屏幕尺寸来计算有多少个SwiperSlide,每个SwiperSlide里面有多少个数据

返回到第一页

this.swiperDom.slideTo(0);

render()

import { Component } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Autoplay, Navigation, Pagination } from 'swiper/core';
import 'swiper/css';
import 'swiper/css/bundle';
render() {
        let { templateList, isMobile } = this.props;
        let { isBegin, isEnd } = this.state;
        let classLeft = classname(Style.preBtn, Style.btn, isBegin ? Style.none : '', 'swiper-button-left', {
            [Style.mobileLeft]: isMobile,
        });
        let classRight = classname(Style.nextBtn, Style.btn, isEnd ? Style.none : '', 'swiper-button-right', {
            [Style.mobileRight]: isMobile,
        });

        return (
            <div className={Style.content_box}>
                <Swiper
                    // spaceBetween={spaceBetween}
                    slidesPerView={1}
                    pagination={{
                        clickable: true,
                    }}
                    onSwiper={swiper => {
                        this.swiperDom = swiper;
                    }}
                    modules={[Navigation]}
                    navigation={{
                        nextEl: '.swiper-button-right',
                        prevEl: '.swiper-button-left',
                    }}
                    onSlideChange={data => {
                        this.setState({
                            isBegin: data.isBeginning,
                            isEnd: data.isEnd,
                        });
                    }}
                    className={classname(Style.swiper, 'swiper')}
                >
                    {templateList.map((item, index) => {
                        let isEnd = index === templateList.length - 1;
                        return (
                            <SwiperSlide
                                key={index}
                                className={classname(Style.swiperSlide, { [Style.swiperSlide_start]: isEnd })}
                            >
                                {this._drawItem(item, isEnd)}
                            </SwiperSlide>
                        );
                    })}
                    <div className={classLeft}>
                        <FJSvg src={arrowRight} className={Style.leftIcon} />
                    </div>
                    <div className={classRight}>
                        <FJSvg src={arrowRight} />
                    </div>
                </Swiper>
            </div>
        );
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值