index.js 1.79 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5
import React, { Component } from "react"
import Swiper from "swiper"
import "./index.scss"
import "swiper/dist/css/swiper.min.css"
import { Link } from "react-router-dom"
FE committed
6 7

class Index extends Component {
zhanghaozhe committed
8 9
  container = null
  swiper = null
FE committed
10

zhanghaozhe committed
11 12
  componentDidMount() {
    if (this.props.bannerList.length > 1) {
zhanghaozhe committed
13 14
      this.swiper = new Swiper(".swiper-container", {
        direction: "horizontal",
zhanghaozhe committed
15 16 17
        loop: true,
        spaceBetween: 12,
        centeredSlides: true,
zhanghaozhe committed
18
        slidesPerView: "auto",
zhanghaozhe committed
19 20 21
        loopedSlides: 3,
        autoplay: true,
      })
FE committed
22
    }
zhanghaozhe committed
23
  }
FE committed
24

zhanghaozhe committed
25
  componentWillUnmount() {
xuzhenghua committed
26
    this.swiper && this.swiper.destroy()
zhanghaozhe committed
27
  }
FE committed
28

zhanghaozhe committed
29
  render() {
zhanghaozhe committed
30
    const { bannerList } = this.props
zhanghaozhe committed
31
    return (
zhanghaozhe committed
32 33
      <div className={"index-banner-swiper"}>
        <div className={"swiper-container"} ref={(el) => (this.container = el)}>
zhanghaozhe committed
34
          <ul className="swiper-wrapper">
zhanghaozhe committed
35 36 37
            {bannerList &&
              bannerList.length > 0 &&
              bannerList.map((item, index) => {
zhanghaozhe committed
38
                return (
zhanghaozhe committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
                  <li className={"swiper-slide"} key={index}>
                    {Number.isNaN(parseInt(item.jump_url)) ? (
                      <a href={item.jump_url} key={index}>
                        <img className="item" src={item.name} alt="" />
                      </a>
                    ) : (
                      <Link
                        to={{
                          pathname: "/detail",
                          search: `?id=${item.jump_url}`,
                        }}
                        key={index}
                      >
                        <img className="item" src={item.name} alt="" />
                      </Link>
                    )}
zhanghaozhe committed
55 56
                  </li>
                )
zhanghaozhe committed
57
              })}
zhanghaozhe committed
58 59 60
          </ul>
        </div>
      </div>
zhanghaozhe committed
61
    )
zhanghaozhe committed
62
  }
FE committed
63 64
}

zhanghaozhe committed
65
export default Index