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

相关推荐
2601_9498095918 分钟前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
Up九五小庞28 分钟前
开源埋点分析平台 ClkLog 本地部署 + Web JS 埋点测试实战--九五小庞
前端·javascript·开源
摘星编程1 小时前
React Native + OpenHarmony:UniversalLink通用链接
javascript·react native·react.js
qq_177767371 小时前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
烬头88211 小时前
React Native鸿蒙跨平台应用实现了onCategoryPress等核心函数,用于处理用户交互和状态更新,通过计算已支出和剩余预算
前端·javascript·react native·react.js·ecmascript·交互·harmonyos
程序员清洒3 小时前
Flutter for OpenHarmony:Text — 文本显示与样式控制
开发语言·javascript·flutter
雨季6663 小时前
Flutter 三端应用实战:OpenHarmony 简易“动态内边距调节器”交互模式深度解析
javascript·flutter·ui·交互·dart
2601_949593654 小时前
基础入门 React Native 鸿蒙跨平台开发:卡片组件
react native·react.js·harmonyos
天人合一peng4 小时前
Unity中button 和toggle监听事件函数有无参数
前端·unity·游戏引擎
会飞的战斗鸡4 小时前
JS中的链表(含leetcode例题)
javascript·leetcode·链表