当前位置: 首页 > 工具软件 > Cover Flow > 使用案例 >

React使用Swiper实现3D coverflow 效果

洪飞驰
2023-12-01

下面是.ts文件代码

import React, { useEffect } from 'react';
import classnames from 'classnames';
import { Row } from 'antd';
import styles from './index.less';
const Videoportal = () => {
  
  useEffect(() => {
    const a = new Swiper('.swiper-container2', {
      effect: 'coverflow',
      grabCursor: true,
      centeredSlides: true,
      initialSlide: 2,
      slidesPerView: 'auto',
      // slidesPerView: 3,
      coverflowEffect: {
        rotate: 50,
        stretch: 90,
        depth: 100,
        modifier: 1,
        slideShadows: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  }, []);

  return (
    <Row className={styles.videoContainer}>
      <Row className={styles.videoBox}>
        <div className={styles.title}>视频门户</div>
        <div
          className={classnames({
            'swiper-container2': true,
            [styles.swiper]: true,
          })}
        >
          <div
            className={classnames({
              'swiper-wrapper': true,
              [styles.wrapper]: true,
            })}
          >
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide1]: true,
              })}
            ></div>
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide2]: true,
              })}
            ></div>
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide3]: true,
              })}
            ></div>
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide4]: true,
              })}
            ></div>
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide5]: true,
              })}
            ></div>
            <div
              className={classnames({
                'swiper-slide': true,
                [styles.slide6]: true,
              })}
            ></div>
          </div>
        </div>
      </Row>
      <Row className={styles.occlusion_left}></Row>
      <Row className={styles.occlusion_right}></Row>
      <div
        className={classnames({
          'swiper-button-prev': true,
          [styles.prev]: true,
        })}
      ></div>
      <div
        className={classnames({
          'swiper-button-next': true,
          [styles.next]: true,
        })}
      ></div>
    </Row>
  );
};

export default Videoportal;

.less文件代码

.videoContainer {
  width: 100%;
  height: 100%;
  .videoBox {
    width: 100%;
    height: 100vh;
    background-color: #fff;
    user-select: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      padding-top: 80px;
      width: 100%;
      font-size: 34px;
      font-weight: bold;
      color: #333333;
    }
      background-color: #fff;
      overflow: hidden;
      width: 950px;
      .swiper {
        padding-top: 100px;
        width: 100%;
        .wrapper {
          width: 100%;
          margin-top: 100px;
        }
        .slide1 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide2 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide3 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide4 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide5 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide6 {
          background-image: url(../../../../assets/portal/bg1.png);
        }
        .slide1,
        .slide2,
        .slide3,
        .slide4,
        .slide5,
        .slide6 {
          background-position: center;
          background-size: cover;
          width: 400px;
          height: 318px;
        }

        :global(.swiper-slide-shadow-left) {
          background-image: none !important;
        }
        :global(.swiper-slide-shadow-right) {
          background-image: none !important;
        }
    }
  }
}
.prev {
  z-index: 3000;
  position: absolute;
  left: 100px;
  background-image: url(../../../../assets/portal/left.png);
}
.next {
  position: absolute;
  right: 100px;
  background-image: url(../../../../assets/portal/right.png);
}

另外需要在document文件中导入Swiper的js文件和css文件

<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.css">

这个是云上的CSS文件

<script src="https://unpkg.com/swiper/swiper-bundle.min.js"> </script>

这是云上的js文件,以上都是Swiper6的,可以自己下载下来放在自己的CDN上面

最后一定要注意:如果需要new多个实例,每个的名字不能一样,否则会覆盖掉上一个,可以用不同的选择器来作为每个实例的标识,解决

 类似资料: