在react里使用swiper基本配置

swiper基本配置

  1. 安装插件
npm i swiper
  1. 页面引入
import { Swiper, SwiperSlide } from 'swiper/react';
import { Navigation, Pagination, EffectCreative, Scrollbar, A11y } from 'swiper';
  1. 样式引入
    可以在项目全局的文件引入css
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'

有可能报错文件找不到,也可以引入这样文件

import 'swiper/swiper.min.css';
const SwiperPage = () => {
  const bannerImg = [
        { color: '#f7f7af' },
        { color: "#8585f9" },
        { color: "#ff4a4a" },
        { color: "#ffff5d" },
        { color: "#62f9a6" },
    ]
     return (
        <Layout style={{ backgroundColor: '#ccc' }}>
            <div >
        <Swiper
                loop={true} //设置循环轮播
                className="mySwiper"
                spaceBetween={-200} //设置堆叠轮播,item之间叠的距离
                slidesPerView="auto" //设置显示的数量
                navigation={true} //modules上加了同时要设置为true,才显示
                modules={[Navigation,Pagination,EffectCreative]}
                grabCursor={true}
                effect={'creative'} //modules上加了同时要设置
                centeredSlides={true} // 居中的slide是否标记为active,默认是最左active,这样样式即可生效
                slideToClickedSlide={false} // 点击的slide会居中
                creativeEffect={{
                  prev: {
                    //这里是设置当前item的前一项的具体属性
                    translate: [-200, 0, 0], //偏移量,三个轴,X、Y、Z
                    scale: 0.8, //缩放量
                    // opacity:0.8,//透明度
                    // shadow:true,//是否加阴影
                    //rotate:30,//item旋转角度
                    //origin:'left bottom'//item位于左下还是哪里
                  },
                  next: {
                    //这里是设置当前item的后一项的具体属性,同上面
                    translate: [200, 0, 0],
                    scale: 0.8,
                    // opacity:0.8,
                    // shadow:true,
                  },
                  limitProgress: 2, //显示五个堆叠的最重要的这个属性,后面依次以前面属性等比配置
                  // shadowPerProgress:true,//是否等比配置透明度
                }}
               onClick={(swiper,event) => {
             //点击事件
              //回调函数,当你点击或轻触Swiper 后执行,相当于tap。
              //接受swiper实例和touchend事件作为参数。
              //Swiper5版本之前会有300ms延迟。
                }}
            onTouchEnd={(swiper,event) => {
              //回调函数,当碰触到slider时执行。可选Swiper实例和touchstart事件作为参数。
                }}
                onTouchEnd={(swiper,event) => {
              //回调函数,触摸释放时执行,接受swiper实例和 touchend事件作为参数。(释放即执行)
                }}

              >
                {bannerImg.map((item, index) => (
                  <SwiperSlide key={index} style={{ width: 360 }}>
                    <div
                      onClick={(e) => {
                        swiperClick(e,index)
                      }}
                      className="swiper-img"
                      style={{ backgroundColor: `${item.color}`, width: '100%', height: '100%' }}
                    ></div>
                  </SwiperSlide>
                ))}
              </Swiper>
        </div>
        </Layout>
    )

}
export default SwiperPage;
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不听念经

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值