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

zhanghaozhe committed
7

zhanghaozhe committed
8
class Index extends Component {
zhanghaozhe committed
9
    container = null
zhanghaozhe committed
10
    swiper = null
zhanghaozhe committed
11 12 13 14

    componentDidMount() {
        this.swiper = new Swiper('.swiper-container', {
            direction: 'horizontal',
zhanghaozhe committed
15
            loop: true,
zhanghaozhe committed
16 17 18
            spaceBetween: 12,
            centeredSlides: true,
            slidesPerView: 'auto',
zhanghaozhe committed
19
            loopedSlides: 3,
zhanghaozhe committed
20
            autoplay: true
zhanghaozhe committed
21 22 23
        })
    }

zhanghaozhe committed
24 25 26 27 28
    componentWillUnmount() {
        this.swiper.destroy()
    }


zhanghaozhe committed
29 30 31
    render() {
        const {bannerList} = this.props
        return (
zhanghaozhe committed
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
            <div className={'index-banner-swiper'}>
                <div className={'swiper-container'} ref={el => this.container = el}>
                    <ul className="swiper-wrapper">
                        {bannerList && bannerList.length > 0 && bannerList.map((item, index) => {
                            return (
                                <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>
                                    }
                                </li>
                            )
                        })
                        }
                    </ul>
                </div>
zhanghaozhe committed
63 64 65 66 67
            </div>
        );
    }
}

zhanghaozhe committed
68
export default Index;