Swiper组件使用-react

声明

import 'swiper/components/navigation/navigation.scss';
import 'swiper/swiper.scss';
import SwiperCore, { Autoplay } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/react';
SwiperCore.use([Autoplay]);  //需要自动播放要加Autoplay,需要前进后退按钮加Navigation

使用

<Swiper spaceBetween={20} slidesPerView={1} loop={true} autoplay initialSlide={1}>
  {list.map((item, index) => (
    <SwiperSlide key={index}>
      内容{item}
    </SwiperSlide>
  ))}
</Swiper>

参数说明

<Swiper
  spaceBetween={20} //间距
  slidesPerView={2} //显示的个数
  direction={'vertical'} //定义横向还是竖向,默认横向,这里vertival为竖向
  autoplay={{ //autoplay即可使用默认数据进行轮播
    delay: 3000, //轮播间隔,默认3000
    stopOnLastSlide: false, //是否最后一项轮播后停止,默认false,最后一项后会重置到第一个,loop=true时该属性无效
    disableOnInteraction: false, //手动触发轮播后,是否停止自动轮播,默认为true,鼠标操作后会停止
  }}
  speed={800} //滑动速度,800毫秒滑动一项,默认为300
  loop={true} //是否循环,循环原理是复制swiperslide,默认false
  initialSlide={2} //起始位置,需要手动调至第一项(没懂规律)
  breakpoints={{
  	// 宽高比大于等于1.7时显示3个,大于等于1.9时显示2个;只有部分不需要变换布局方式和逻辑结构的参数支持断点设定,如slidesPerView、slidesPerGroup、 spaceBetween,而像slidesPerColumn、loop、direction、effect等则无效
   '@1.7': { slidesPerView: 3 },
   '@1.9': { slidesPerView: 2 },
  }}
  onSwiper={(swiper) => {
    //鼠标悬浮暂停效果
    swiper.$el[0].addEventListener('mouseover', () => swiper.autoplay.stop());
    //鼠标移开后继续自动滚屏效果
    swiper.$el[0].addEventListener('mouseleave', () => swiper.autoplay.start());
  }}
>

样式调整

根据实际宽高设置swiper最外层宽高

.swiper-container {
    width: 100%;
    height: 100%;
}

不能手动调整每一项的css大小/定位/边距等样式,否则滚动会出问题,所有调整查看文档对应参数解决。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React函数组件中,我们可以使用React Hooks来替代componentDidMount()生命周期方法。同时,可以使用useState来管理组件的状态。下面是修改后的代码: ```javascript import React, { useEffect, useState } from 'react'; function MyComponent() { const [appAuthLength, setAppAuthLength] = useState(0); const [userinfo, setUserinfo] = useState(null); useEffect(() => { const fetchData = async () => { const data = JSON.parse(sessionStorage.getItem('retstring')); setUserinfo(data.userinfo[0]); setAppAuthLength(data.app_auth_length); }; fetchData(); }, []); useEffect(() => { const swiperOptions = { loop: false, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, slidesPerView: 'auto', spaceBetween: 80, centeredSlides: true, loopedSlides: 9, }; if (userinfo.role === '财务') { swiperOptions.loop = false; } else if (appAuthLength > 2) { swiperOptions.loop = true; } new Swiper('.swiper-container', swiperOptions); }, [userinfo.role, appAuthLength]); return ( <div className="swiper-container"> {/* Swiper slides */} </div> ); } export default MyComponent; ``` 在上面的代码中,我们使用了两个useEffect钩子函数。第一个useEffect用来获取并设置初始状态,模拟componentDidMount的功能。第二个useEffect用来监听状态的变化,并根据条件设置Swiper的选项。最后,将Swiper组件渲染到页面中。 请注意,为了正确使用Hooks,我们需要将代码封装在函数组件中,并使用`import React, { useEffect, useState } from 'react';`引入React和Hooks。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值