温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

react如何实现淡入淡出

发布时间:2022-12-28 14:02:32 阅读:178 作者:iii 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“react如何实现淡入淡出”,在日常操作中,相信很多人在react如何实现淡入淡出问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”react如何实现淡入淡出”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

react实现淡入淡出的方法:1、通过“npm i react-addons-css-transition-group”下载“react-addons-css-transition-group”库;2、通过“render(){return(<div className="list" onMouseLeave={this.start.bind...}”方式实现淡入淡出即可。

react实现淡入淡出

首先要下载一个库,即npm i react-addons-css-transition-group,接下来看代码

//Pic.jsimport React,{Component} from 'react'import Img1 from '../../image/1.jpg'import Img2 from '../../image/2.jpg'import Img3 from '../../image/3.jpg'import Image from './Image'import Dot from './Dot'import './pic.css'class Pic extends Component{    constructor(props){        super(props)        this.state={current:0,            src:[Img1,Img2,Img3]        }        this.timer=null;    }    componentDidMount(){        this.timer=setInterval(()=>{            this.setState((prev)=>{return{current:(prev.current+1)%3}})        },3000)    }    clear(){        clearInterval(this.timer);    }//鼠标移入结束动画    start(){        this.timer=setInterval(()=>{            this.setState((prev)=>{return{current:(prev.current+1)%3}})        },3000)    }//鼠标移出开始动画    change=(i)=>{        console.log(i)        this.setState({current:i})    }//鼠标点击原点切换图片    render(){        return(<div className="list"         onMouseLeave={this.start.bind(this)}        onMouseEnter={this.clear.bind(this)}>            <Image src={this.state.src[this.state.current]}             current={this.state.current}            name="item"            component={'li'}            enterDelay={1500}//动画开始所用时间            leaveDelay={1500}//动画结束所用时间            ></Image>            <Dot current={this.state.current} change={this.change}></Dot>        </div>)    }}export default Pic
Image.js

import React,{Componentfrom 'react'
import CSSTransitionGroup from 'react-addons-css-transition-group';
import './pic.css'

class Image extends Component{

    constructor(props){
        super(props)
    }

    render(){
        return(
        <ul>
        <CSSTransitionGroup
               component={this.props.component}
               transitionName={this.props.name}
               transitionEnterTimeout={this.props.enterDelay}
               transitionLeaveTimeout={this.props.leaveDelay}
               className={this.props.name}
        >
            <img src={this.props.src}
            key={this.props.src}
            ></img>
        </CSSTransitionGroup>
        </ul>
        )
    }

}

export default Image
Dot.js

import React ,{Componentfrom 'react'

class Dot extends Component{
    constructor(props){
        super(props)
        this.state={arr:[1,2,3]}
    }
    render(){
        return(<div className="dot">
            <ul>
            {this.state.arr.map((item,index)=>{
                return(<li 
                    onClick={this.props.change.bind(this,index)}
                    key={index}
                    className={[index==this.props.current?'current':'']}></li>)
            })}
            </ul>
        </div>)
    }
}

export default Dot
//css样式
*{margin:0;padding:0;}
.list{width:500px;
     height:400px;
     margin:30px auto;
}
ul{position: relative;
   width:500px;
   height:400px;
   overflow: hidden;
}
li{
    position: absolute;
    list-style: none;}
img{width:500px;
    height:400px;
    }
.item-enter{
    position: absolute;
    opacity0;

}
.item-enter-active{opacity:1;
    transition1.5s opacity ease-in-out;
}
.item-leaveposition: absolute;
    opacity1;}
.item-leave-active{
    opacity0;
    transition1.5s opacity ease-in-out;
}

.dot{
    position: absolute;
    top:380px;
    left:250px;
  
    width:150px;
    height:50px;
}
.dot ul{width:100%;
        height:100%;
}
.dot li{
    position: static;
    float:left;
    margin-left:10px;
    width:25px;
    height:25px;
    border-radius50%;
    border:1px solid deeppink;
    transition:3s;
    list-style:none;}

.current{background-color: gold;}

到此,关于“react如何实现淡入淡出”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×