react轮播图示例

index.tsx

复制代码
import React, { useRef, useEffect, useState } from 'react'

import './swiper.less'
import _ from 'lodash'
import img1 from 'assets/img/1.jpg'
import img2 from 'assets/img/2.jpg'
import img3 from 'assets/img/3.jpg'
import img4 from 'assets/img/4.jpg'
import { Button } from 'antd'


export default () => {

    const imgList = [
        {
            img: img1
        },
        {
            img: img2
        },
        {
            img: img3
        },
        {
            img: img4
        }
    ]
    const [isShow, setIsShow] = useState(true)

    const ref = useRef<any>({
        current: { currentIndex: 0, runIn: null }
    })

    const getPrevIndex = (nowIndex) => {//当下标是第0个,前一个就是最后一个
        if (nowIndex === 0) {
            return imgList.length - 1
        }
        else {
            return nowIndex - 1
        }
    }
    const getNextIndex = (nowIndex) => {//当前下标是最后一个,后一个下标就是第0个
        if (nowIndex === imgList.length - 1) {
            return 0
        }
        else {
            return nowIndex + 1
        }
    }

    function goto(index) {
        setIsShow(false)
        setTimeout(() => {
            setIsShow(true)
            ref.current.currentIndex = index
        }, 200)
    }

    function slideClick(i) {
        clear()
        goto(i)
    }
    function play() {
        if (ref.current.runIn) clear()
        ref.current.runIn = setInterval(() => {
            goto(getNextIndex(ref.current.currentIndex))
        }, 1000)
    }

    function clear() {
        clearInterval(ref.current.runIn)
    }

    function goLeft() {
        clear()
        goto(getPrevIndex(ref.current.currentIndex))
    }

    function goRight() {
        clear()
        goto(getNextIndex(ref.current.currentIndex))
    }

    useEffect(() => {
        if (isNaN(ref.current.currentIndex)) ref.current.currentIndex = 0
        play()
    }, [])

    return (
        <>
            <div>
                <div className="slider">
                    <div onMouseOut={play} onMouseOver={clear}>
                        <Button className="left-btn" onClick={goLeft}>&lt;</Button>
                        <Button className="right-btn" onClick={goRight}>&gt;</Button>
                    </div>

                    <ul className="container" onMouseOut={play} onMouseOver={clear}>
                        <li>
                            {isShow && <img src={_.get(imgList[ref.current.currentIndex], 'img')} alt=""
                            />}
                            {!isShow && <img src={_.get(imgList[ref.current.currentIndex], 'img')} alt=""
                                style={{ transform: 'translateX(200px)', transition: 'all 700ms cubic-bezier(0.19, 1, 0.22, 1)' }}
                            />}
                        </li>
                    </ul>
                </div>

                <ul className="btn-list" onMouseOut={play} onMouseOver={clear}>
                    {_.map(imgList, (_, index) => {
                        return <li v-for="(item, index) in sliders" key={'btn-list_' + index} onClick={() => slideClick(index)}>{index + 1}</li>
                    })}
                </ul>
            </div>
        </>
    )
}

swiper.less

复制代码
* {
    margin: 0;
    padding: 0;
}
.slider {
    position: relative;
}
.container{
    width: 100%;
    display: flex;
    overflow: hidden;
  }
  .left-btn,.right-btn{
    position: absolute;
    margin-top: 100px;
    width: 30px;
    height: 30px;
    text-align: center;
    background-color: pink;
  }
  .left-btn {
    left: 0;
  }
  .right-btn {
    left: calc(100% - 30px);
  }
  ul,li {
    list-style: none;
  }
  .container li {
    width: 100%; 
    height: 33vw;
    overflow: hidden;
  }
  .container li img{
    width: 100%; 
    left: 0;
  }
  .btn-list {
    display: flex;
    align-items: center;
  }
  .btn-list li{
    display: inline-flex;
    align-items: center;
    width: 30px;
    height: 30px;
    padding: 20px;
    background-color: pink;
    margin-left: 10px;
  }

App.tsx使用:

复制代码
import React from 'react';
import Swpier from './pages/swiper';


const App = () => {
  return (
      <Swpier />
  )
}

export default App;

代码仓库地址:

命运推手/my_web

相关推荐
前端逗比逗15 分钟前
Electron 全维度完整配置手册(最新稳定版,适配 Electron 25+)
前端·electron
xiaobaoyu19 分钟前
聊天问答文字逐步显示实现
前端
随风一样自由27 分钟前
【前端+项目分析】`img` vs `Image`:从两个真实项目看前端图片组件的正确选择
前端·image·img·项目对比分析
倾颜28 分钟前
断线之后,不要重跑 AI:在 POST + NDJSON 中实现可恢复 Agent 流
前端·后端·agent
程序员黑豆37 分钟前
鸿蒙应用开发之父子组件传参:@Prop 装饰器使用教程
前端·后端·harmonyos
寒水馨1 小时前
Linux下载、安装 Bun v1.3.14(附安装包bun-linux-x64.zip)
linux·javascript·typescript·node.js·bun·运行时·包管理器
信也科技布道师1 小时前
从绝对定位到可维护页面:一次 MasterGo 还原链路的实战复盘
前端
程序员黑豆1 小时前
鸿蒙应用开发之V2状态管理:@Local、@ObservedV2、@Trace 使用教程
前端·后端·harmonyos
锻炼²1 小时前
Edge 地址栏搜索被其他浏览器劫持
前端·edge
windliang1 小时前
Claude Code 源码分析(一):从 claude 命令到 Agent 主循环
javascript·面试·ai编程