码迷,mamicode.com
首页 > 其他好文 > 详细

React Swiper轮播图

时间:2021-03-29 11:45:53      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ack   wip   mode   play   direction   port   pos   map   styles   

需求

  • 实现React轮播图

使用库

实现方法

/** 组件:图片轮播*/
import React, { Component } from "react";
import SwiperCore, { Navigation, Pagination, Autoplay, Thumbs } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/swiper-bundle.css";
import styles from "./index.less";
SwiperCore.use([Navigation, Pagination, Autoplay, Thumbs]);

interface IProps {
  imgs?: Array<any>;
}

class Gallery extends Component<IProps, any> {
  constructor(props) {
    super(props);
    this.state = {
      swiper: null,
      swiperThumb: null,
      activeIndex: 0,
    };
  }
  render(): JSX.Element {
    const { imgs = [] } = this.props;
    const { activeIndex } = this.state;
    return (
      <div className={styles.picsBox}>
        <div className={styles.picsBig}>
          <Swiper
            autoplay
            navigation
            loopedSlides={5}
            className={styles.swiperBox}
            pagination={{
              type: "fraction",
            }}
            thumbs={{ swiper: this.state?.swiperThumb }}
            onSlideChange={(swiper) => {
              this.setState({ activeIndex: swiper?.activeIndex });
              this.state?.swiperThumb.slideTo(swiper?.activeIndex);
            }}
            onSwiper={(swiper) => this.setState({ swiper })}
          >
            {imgs.map((imgInfo, index) => {
              return (
                <SwiperSlide key={index}>
                  <img src={imgInfo} className={styles.imgBox} />
                </SwiperSlide>
              );
            })}
          </Swiper>
        </div>
        <div className={styles.picsList}>
          <Swiper
            onSwiper={(swiperThumb) => this.setState({ swiperThumb })}
            className={styles.swiperBoxThumb}
            spaceBetween={16}
            slidesPerView={4}
            freeMode={true}
            direction={"vertical"}
            watchSlidesVisibility={true}
            watchSlidesProgress={true}
          >
            {imgs.map((imgInfo, index) => {
              return (
                <SwiperSlide key={index} className={styles.swiperBoxThumbSlide}>
                  {activeIndex !== index ? (
                    <div className={styles.mask}></div>
                  ) : null}
                  <img src={imgInfo} className={styles.thumbImg} />
                </SwiperSlide>
              );
            })}
          </Swiper>
        </div>
      </div>
    );
  }
}

export default Gallery;

.picsBox {
  width: 100%;
  height: 650px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .picsBig {
    width: 900px;
    height: 100%;
  }
  .picsList {
    width: 236px;
    height: 100%;
    overflow: hidden;
  }
  .imgBox {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
  }
}

.swiperBox {
  width: 100%;
  height: 100%;
  color: white;
  text-shadow: 1px 1px 10px rgba(0, 0, 0, 0.3);
  font-size: 16px;
  --swiper-theme-color: white;
}

.swiperBoxThumb {
  width: 236px;
  height: 650px;
  .swiperBoxThumbSlide {
    position: relative;
    width: 100%;
    height: 150px;
    overflow: hidden;
    cursor: pointer;
  }
  .thumbImg {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .mask {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.3);
  }
}
.swiper-slide-thumb-active {
  opacity: 1 !important;
}

效果预览

技术图片

React Swiper轮播图

标签:ack   wip   mode   play   direction   port   pos   map   styles   

原文地址:https://www.cnblogs.com/KevinTseng/p/14583258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!