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;

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React 实现轮播图效果可以通过以下步骤实现: 1. 创建轮播组件:创建一个轮播组件来包含所有的图片和轮播图的逻辑。 2. 渲染图片:在轮播组件中定义一个数组,用来存放所有的图片。使用 map 方法来遍历这个数组,渲染所有的图片。 3. 定义状态:在轮播组件中定义一个状态,用来记录当前显示的图片的索引。初始值为0。 4. 轮播逻辑:使用定时器和 setState 方法来实现轮播逻辑。每隔一段时间,将当前显示的图片索引加1,然后调用 setState 方法来更新状态。当索引大于等于图片数组的长度时,将索引重置为0。 5. 添加控制按钮:可以添加左右控制按钮来控制轮播图的前进和后退。点击按钮时,调用 setState 方法来更新状态。 6. 完成轮播组件:将所有的逻辑和 UI 结合起来,完成轮播组件的编写。 以下是一个简单的轮播组件代码示例: ```javascript import React, { useState, useEffect } from 'react'; const Carousel = ({ images }) => { const [activeIndex, setActiveIndex] = useState(0); useEffect(() => { const interval = setInterval(() => { setActiveIndex(activeIndex => (activeIndex + 1) % images.length); }, 3000); return () => clearInterval(interval); }, [images.length]); const onPrevClick = () => { setActiveIndex(activeIndex => (activeIndex - 1 + images.length) % images.length); }; const onNextClick = () => { setActiveIndex(activeIndex => (activeIndex + 1) % images.length); }; return ( <div className="carousel"> <div className="carousel-images"> {images.map((src, index) => ( <img key={index} src={src} alt={`Image ${index}`} className={index === activeIndex ? 'active' : ''} /> ))} </div> <button onClick={onPrevClick}>Prev</button> <button onClick={onNextClick}>Next</button> </div> ); }; export default Carousel; ``` 在这个例子中,使用了 useState 和 useEffect 钩子函数来实现状态和定时器的功能。渲染图片时使用了 map 方法,根据当前显示的索引来给图片添加 active 类名,从而实现高亮显示。左右控制按钮的点击事件分别调用 onPrevClick 和 onNextClick 方法,通过 setActiveIndex 方法来更新状态。最后,将所有的 UI 和逻辑结合起来,形成一个完整的轮播组件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LLL_LH

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

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

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

打赏作者

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

抵扣说明:

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

余额充值