上代码
1、 页面引入swiper
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Autoplay, Navigation, Pagination } from 'swiper/core';
import 'swiper/swiper.min.css';
import 'swiper/swiper.scss';
import 'swiper/components/pagination/pagination.scss'
SwiperCore.use([Autoplay, Pagination, Navigation])
2、代码使用(轮播滑块1个时,则不展示小圆点)
<Swiper
slidesPerView={1}
autoplay={{delay: 3000,disableOnInteraction: false}}
spaceBetween={10}
navigation
pagination={{ clickable: true }}
height={300}>
{
// @ts-ignore
store.vesselPhoto.map((item: string, index: any) => (
<SwiperSlide>
<img alt='' key={item} src={item}></img>
</SwiperSlide>
))
}
</Swiper>
如需修改默认显示某页
1、需获取swiper实例
const [swiperDom ,setSwiperDom ] = useState(null)
<Swiper
// slidesPerView={1}
initialSlide={numberList[checkedIndex]} // 默认页只能是number数字,不可使用Number
centeredSlides={true}
spaceBetween={10}
navigation
onSwiper={(swiper) => {
swiperDom = swiper; // 获取swiper实例 重点
}}
onSlideChange={(e) =>{
checkedIndex = e.activeIndex
setTimeout(() => {
if (swiperDom) {
swiperDom.slideTo(e.activeIndex,500,false); // 使用swiper'中slideTo方法进行切换默认页
}
}, 300);
}}
pagination={{ clickable: true }}
className='swiperBox'
>
</Swiper>