溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

react如何實現(xiàn)淡入淡出

發(fā)布時間:2022-12-28 14:02:32 來源:億速云 閱讀:149 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“react如何實現(xiàn)淡入淡出”,在日常操作中,相信很多人在react如何實現(xiàn)淡入淡出問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”react如何實現(xiàn)淡入淡出”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

react實現(xiàn)淡入淡出的方法:1、通過“npm i react-addons-css-transition-group”下載“react-addons-css-transition-group”庫;2、通過“render(){return(<div className="list" onMouseLeave={this.start.bind...}”方式實現(xiàn)淡入淡出即可。

react實現(xiàn)淡入淡出

首先要下載一個庫,即npm i react-addons-css-transition-group,接下來看代碼

//Pic.js

import 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);
   }//鼠標(biāo)移入結(jié)束動畫
   start(){
       this.timer=setInterval(()=>{
           this.setState((prev)=>{return{current:(prev.current+1)%3}})
       },3000)
   }//鼠標(biāo)移出開始動畫
   change=(i)=>{
       console.log(i)
       this.setState({current:i})
   }//鼠標(biāo)點擊原點切換圖片

   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}//動畫結(jié)束所用時間
           ></Image>
           <Dot current={this.state.current} change={this.change}></Dot>
       </div>)


   }

}
export default Pic

Image.js

import React,{Component} from '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 ,{Component} from '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;
    opacity: 0;

}
.item-enter-active{opacity:1;
    transition: 1.5s opacity ease-in-out;
}
.item-leave{ position: absolute;
    opacity: 1;}
.item-leave-active{
    opacity: 0;
    transition: 1.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-radius: 50%;
    border:1px solid deeppink;
    transition:3s;
    list-style:none;}

.current{background-color: gold;}

到此,關(guān)于“react如何實現(xiàn)淡入淡出”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI