• React Swiper轮播图(二)


    目录

    需求

    • 实现React可切换轮播图
    • 效果预览

    使用库

    实现方法

    /** 导航 */
    import React, { useState } from "react";
    import SwiperCore, { Navigation, Pagination, Autoplay, Thumbs } from "swiper";
    import { Swiper, SwiperSlide } from "swiper/react";
    import Icon from "./component/icon";
    import LazyImage from "./component/lazyImage";
    import { navCategoryImgs, navStyleImgs } from "./imgs";
    
    import {
      NavType,
      NavImgType,
      QueryType,
    } from "./types";
    
    import styles from "./index.less";
    import "swiper/swiper-bundle.css";
    
    SwiperCore.use([Navigation, Pagination, Autoplay, Thumbs]);
    
    interface IProps {
      query?: QueryType;
      onSwitchNav?: (navInfo: NavImgType) => void;
    }
    function NavSwiper(props: IProps): JSX.Element {
      const [swiper, setSwiper] = useState<SwiperCore | null>(null);
      const [activeIndex, setActiveIndex] = useState<number>(0);
      const [hasInit, setHasInit] = useState<boolean>(false);
    
      const { by, option } = props?.query || {};
      let navImgs: NavImgType[] = [];
      switch (by) {
        case NavType.category:
          navImgs = navStyleImgs.concat(navCategoryImgs);
          break;
        case NavType.style:
        default:
          navImgs = navCategoryImgs.concat(navStyleImgs);
          break;
      }
    
      // 上一屏
      function slideToNext() {
        swiper?.slideNext();
      }
    
      // 下一屏
      function slideToPrev() {
        swiper?.slidePrev();
      }
    
      // 点击类目
      function switchNav(navInfo: NavImgType) {
        props?.onSwitchNav && props.onSwitchNav(navInfo);
      }
    
      return (
        <Swiper
          className={styles.navSwiper}
          loop
          loopedSlides={5}
          slidesPerView={5}
          slidesPerGroup={5}
          spaceBetween={16}
          onSlideChange={(swiper) => setActiveIndex(swiper?.activeIndex)}
          onSwiper={(swiper) => setSwiper(swiper)}
          onInit={() => setHasInit(true)}
        >
          {!hasInit ? (
            <LazyImage className={styles.navLoading} />
          ) : (
            <React.Fragment>
              {navImgs.map((img, key) => {
                return (
                  <SwiperSlide key={key} className={styles.navSlide}>
                    <img
                      src={img?.src}
                      onClick={() => {
                        switchNav(img);
                      }}
                    />
                  </SwiperSlide>
                );
              })}
            </React.Fragment>
          )}
          {hasInit && (
            <React.Fragment>
              <span
                slot="container-start"
                className={styles.navPrev}
                onClick={slideToPrev}
              >
                <Icon name="ArrowLeft" />
              </span>
              <span
                slot="container-end"
                className={styles.navNext}
                onClick={slideToNext}
              >
                <Icon name="ArrowRight" />
              </span>
            </React.Fragment>
          )}
        </Swiper>
      );
    }
    
    export default NavSwiper;
    
    .navSwiper {
      user-select: none;
      .navLoading {
         100%;
        height: 160px;
      }
      .navSlide {
         227px;
        height: 160px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        &:hover {
          opacity: 0.9;
        }
      }
      .navPrev,
      .navNext {
        position: absolute;
        top: 56px;
         24px;
        height: 48px;
        text-align: center;
        font-size: 24px;
        line-height: 46px;
        color: #aaaaaa;
        background: rgba(0, 0, 0, 0.2);
        z-index: 2;
        cursor: pointer;
        &:hover {
          opacity: 0.9;
        }
      }
      .navPrev {
        left: 0;
      }
      .navNext {
        right: 0;
      }
    }
    
  • 相关阅读:
    python手写神经网络实现识别手写数字
    记录:tensoflow改错TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a float into a Te
    6 TensorFlow实现cnn识别手写数字
    记录:python读取excel文件
    matlab手写神经网络实现识别手写数字
    把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中
    Mac卸载go
    vue中axios的post和get请求示例
    vue配置请求拦截器和响应拦截器
    vue中main.js配置后端请求地址
  • 原文地址:https://www.cnblogs.com/KevinTseng/p/15193733.html
Copyright © 2020-2023  润新知