轮播图---swiper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { useState } from "react";
import SwiperCore, { Autoplay, Pagination } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react/swiper-react.js";
 
import "swiper/swiper.scss";
import "swiper/modules/navigation/navigation.scss";
import "swiper/modules/pagination/pagination.scss";
import styles from "./index.module.scss";
 
SwiperCore.use([Autoplay, Pagination]);
 
interface PropsType {
  children: Array<JSX.Element>;
  className?: string;
  swiperConfig?: any;
  accessoryRenderer?: (data: { curIndex: number }) => JSX.Element;
  onSlideDone?: (e) => void;
}
export default (props: PropsType) => {
  const [sliderIndex, setSliderIndex] = useState(0);
 
  const {
    children,
    className = "",
    swiperConfig = {},
    accessoryRenderer,
    onSlideDone,
  } = props;
 
  const swiperConfigResult = Object.assign(
    {
      autoplay: false,
      // autoplay:false,
      spaceBetween: 1,
      loop: false,
      allowTouchMove: true,
      noSwiping: true,
      pagination:
        children.length > 1
          ? { el: ".swiper-pagination", clickable: true }
          : false,
    },
    swiperConfig
  );
 
  return (
    <div className={"service-carousel " + className}>
      <Swiper
        {...swiperConfigResult}
        onSlideChange={(slide) => {
          setSliderIndex(slide.realIndex);
          if (onSlideDone) {
            slide.realIndex === 1 ? onSlideDone(1) : onSlideDone(0);
          }
        }}
      >
        {props.children.map((item, index) => {
          return <SwiperSlide key={index}>{item}</SwiperSlide>;
        })}
        {children.length > 1 && (
          <div
            className={[styles.swiperPagination, "swiper-pagination"].join(" ")}
          ></div>
        )}
      </Swiper>
 
      {/* 辅助渲染下标的 */}
      {accessoryRenderer ? accessoryRenderer({ curIndex: sliderIndex }) : null}
    </div>
  );
};

 

一些swiper网站地址

1
2
3
4
5
6
7
https://www.patreon.com/swiperjs
 
https://swiperjs.com/swiper-api#pagination
 
https://swiperjs.com/react
 
https://www.swiper.com.cn/usage/index.html

  

posted @   木杉呀  阅读(58)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示