React实现轮播组件

一个简单的demo,react写轮播组件

1、SwipePlayer.js

import React from 'react';
import './demo.css'
class SwipePlayer extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      index: 0 // 当前轮播第几个图片
    }

  }

  // 点击播放下一张
  _nextImg () {
    var { index } = this.state;
    if (index === this.props.images.length - 1) {
      index = 0;
    } else {
      index++
    }
    this.setState({
      index: index
    })
  }

  // 点击播放上一张
  _prevImg () {
    var { index } = this.state;
    if (index === 0) {
      index = this.props.images.length - 1
    } else {
      index--
    }
    this.setState({
      index: index
    })
  }

  // 当鼠标停留在图片上时
  mouseHoverImg () {
    clearInterval(this.timerId)
  }
  mouseLeaveImg () {
    this.play()
  }

  play () {
    this.timerId = setInterval(() => {
      this._nextImg()
    }, 3000)
  }

  componentDidMount () {
    this.play()
  }
  componentWillUnmount () {
    clearInterval(this.timerId)
  }

  render () {
    var { index } = this.state;
    return (
      <div className="wrap">
        <ul className="list">
          {
            this.props.images.map((item, i) => (
              <li className={`item ${i === index ? 'active' : ''}`} key={i}
                onMouseOver={() => this.mouseHoverImg()}
                onMouseLeave={() => this.mouseLeaveImg()}>
                <img src={item} alt="" />
              </li>
            ))
          }
        </ul>
        <button className="btn left" onClick={() => this._prevImg()} onMouseOver={() => this.mouseHoverImg()}
          onMouseLeave={() => this.mouseLeaveImg()}>&lt;</button>
        <button className="btn rigth" onClick={() => this._nextImg()} onMouseOver={() => this.mouseHoverImg()}
          onMouseLeave={() => this.mouseLeaveImg()}>&gt;</button>
      </div>
    )
  }
}

export default SwipePlayer;

2、demo.css

ul{
  list-style: none;
  margin: 0px;
  padding: 0px;
}
.wrap{
  position: relative;
  width: 1000px;
  height: 600px;
}
.list{
  position: relative;
  width: 100%;
  height: 100%;
}
.list .item{
  position: absolute;
  top: 0px;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}
.list .item.active{
  z-index: 10;
}
.list .item img{
  width: 100%;
}
.wrap .btn{
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 60px;
  height: 100px;
  background: rgba(0,0,0,.5);
  color: #fff;
  border: none;
  cursor: pointer;
  transition: all .3s;
  z-index: 20;
}
.wrap .btn:hover{
  background: rgba(0,0,0,.8);
}
.wrap .btn.left{
  left: 0px;
}
.wrap .btn.rigth{
  right: 0px;
}

3、父组件

import React from 'react';
import image_1 from './images/java.jpg'
import image_2 from './images/sakula.jpg'
import image_3 from './images/timg.jpg'
import SwipePlayer from './SwipePlayer'

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      images: [image_1, image_2, image_3]
    }
  }

  render () {
    return (
      <div>
        <SwipePlayer images={this.state.images}></SwipePlayer>
      </div>
    )
  }
}

export default App;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLL_LH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值