react使用CSS實(shí)現(xiàn)react動(dòng)畫功能示例
本文實(shí)例講述了react使用CSS實(shí)現(xiàn)react動(dòng)畫功能。分享給大家供大家參考,具體如下:
react動(dòng)畫:
import React, { Component } from 'react';
class Boss extends Component {
constructor(props) {
super(props);
this.state = {
isShow:true
}
this.toTogger=this.toTogger.bind(this)
}
render() {
return (
<div>
<div className={this.state.isShow?'show':'hide'}>大BOSS--孫悟空</div>
<div><button onClick={this.toTogger}>召喚</button></div>
</div>
);
}
toTogger() {
this.setState({
isShow:this.state.isShow?false:true
})
}
}
export default Boss;
css:
.hide{opacity: 1;transition: all 1.5s ease-in;}
.show{opacity: 0;transition: all 1.5s ease-in;}
keyframes動(dòng)畫:
.hide{animation: hide-item 2s ease-in forwards;}
.show{animation: show-item 2s ease-in forwards;}
@keyframes hide-item{
0%{
opacity: 0;
color: red;
}
50%{
opacity: 0.5;
color: saddlebrown;
}
100%{
opacity: 1;
color: yellow;
}
}
@keyframes show-item{
0%{
opacity: 1;
color:green;
}
50%{
opacity: 0.5;
color:greenyellow;
}
100%{
opacity: 0;
color: yellow;
}
}
react-transition-group動(dòng)畫庫:
import {CSSTransition} from 'react-transition-group';
render() {
return (
<div>
<CSSTransition
in={this.state.isShow}
timeout={2000}
classNames="boss-text"
unmountOnExit
>
{/* <div className={this.state.isShow?'show':'hide'}>大BOSS--孫悟空</div> */}
<div>大BOSS--孫悟空</div>
</CSSTransition>
<div><button onClick={this.toTogger}>{this.state.btn}</button></div>
</div>
);
}
.boss-text-enter{opacity: 0;}
.boss-text-enter-active{opacity: 1;transition: opacity 2000ms;}
.boss-text-enter-done{opacity: 1;}
.boss-text-exit{opacity: 1;}
.boss-text-exit-active{opacity: 0;transition: opacity 2000ms;}
.boss-text-exit-done{opacity: 0;}
多DOM動(dòng)畫:
import React, { Component, Fragment } from 'react';
import List from './List.js';
import axios from 'axios';
import Boss from './Boss';
import {CSSTransition,TransitionGroup} from 'react-transition-group'
class Test extends Component {
constructor(props) {
super(props);
this.state={
inputValue:'aaa',
list:[],
}
// this.add=this.add.bind(this);
}
addList() {
this.setState({
list:[...this.state.list,this.state.inputValue],
inputValue:''
})
}
change(e) {
this.setState({
// inputValue:e.target.value
inputValue:this.input.value
})
}
delete(i) {
// console.log(i);
const list = this.state.list;
list.splice(i,1);
this.setState({
list:list
})
}
componentDidMount() {
// console.log('componentDidMount');
axios.get('https://www.easy-mock.com/mock/5e1d3d1564a3c20d7f366f91/ReactDemo1/App')
.then((res)=>{
console.log('獲取數(shù)據(jù)'+JSON.stringify(res));
this.setState({
list:res.data.data
});
})
.catch((error)=>{console.log('獲取數(shù)據(jù)失敗'+error)});
}
render() {
console.log('3-render');
return (
<Fragment>
<div>
<input ref={(input)=>{this.input=input}} value={this.state.inputValue} onChange={this.change.bind(this)}/>
<button onClick={this.addList.bind(this)}>添加</button>
</div>
<ul>
<TransitionGroup>
{
this.state.list.map((v,i)=>{
return(
<CSSTransition
timeout={2000}
classNames='boss-text'
unmountOnExit
key={i}
>
<List key={i} content={v} index={i} delete={this.delete.bind(this)} />
</CSSTransition>
);
})
}
</TransitionGroup>
</ul>
<Boss/>
</Fragment>
);
}
}
export default Test;
希望本文所述對大家react程序設(shè)計(jì)有所幫助。
相關(guān)文章
react native實(shí)現(xiàn)監(jiān)控手勢上下拉動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了react native實(shí)現(xiàn)監(jiān)控手勢上下拉動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
react實(shí)現(xiàn)一個(gè)優(yōu)雅的圖片占位模塊組件詳解
這篇文章主要給大家介紹了關(guān)于react如何實(shí)現(xiàn)一個(gè)還算優(yōu)雅的占位模塊圖片組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
React中使用setInterval函數(shù)的實(shí)例
這篇文章主要介紹了React中使用setInterval函數(shù)的實(shí)例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法
這篇文章主要為大家介紹了ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
react?Scheduler?實(shí)現(xiàn)示例教程
這篇文章主要為大家介紹了react?Scheduler?實(shí)現(xiàn)示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React創(chuàng)建對話框組件的方法實(shí)例
在項(xiàng)目開發(fā)過程中,對于復(fù)雜的業(yè)務(wù)選擇功能很常見,下面這篇文章主要給大家介紹了關(guān)于React創(chuàng)建對話框組件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
react hook使用useState更新數(shù)組,無法更新問題及解決
這篇文章主要介紹了react hook使用useState更新數(shù)組,無法更新問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

