網站首頁 編程語言 正文
1.下載
cnpm install react-transition-group --save
2.配置
在路由配置文件/src/App.js文件下中導入動畫組件
引入
import {TransitionGroup, CSSTransition} from 'react-transition-group'
在路由組件模板中,用動畫組件包裹路由組件
<TransitionGroup>
<CSSTransition timeout={3000} classNames="dg" unmountOnExit key={props.location.pathname}>
<Switch location={props.location}>
<Route path="/" exact component={Login}/>
<Route path="/child1" exact component={Home}/>
<Route path="/child2" exact component={My}/>
</Switch>
</CSSTransition>
</TransitionGroup>
使用動畫的寫法 CSSTransition 需要設置三個屬性:
- in: 控制子元素顯示隱藏的條件,一般是bool值或表達式
- timeout: 入場或出場動畫的時間,單位默認毫秒
- className: 入場或出場class屬性名前綴
注意:CSSTransition組件的key屬性值要保證唯一性,所以用location.pathname
Switch組件要設置location屬性為路由信息的props.location,保證路由跳轉組件的key和CSSTransition的key一致
如果路由中沒有location.pathname,需要使用withRouter導入
import { withRouter } from 'react-router-dom'
export default withRouter(App);
3.引入css
在組件中寫出路由切換動畫的過程
/* 入場動畫過程 */
.dg-enter{
transform: translateX(200px)
}
.dg-enter-active{
transition: 2s;
transform: translateX(0px)
}
.dg-enter-done{
transform: translateX(0px)
}
/* 出場動畫過程 */
.dg-exit{
transform: translateX(0px)
}
.dg-exit-active{
transition: 2s;
transform: translateX(200px)
}
.dg-exit-done{
transform: translateX(200px)
}
引入css文件
import './app.css'
這樣一個簡單的React路由動畫切換就實現了
4.結合animate庫
我們可以自己定義classNames,然后使用css3設置動畫,也可以結合animate庫設置他的動畫效果
下載
npm install animate.css --save
引入
import 'animate.css';
//自定義的類名(定義動畫效果,進場前,進場后直到結束,結束前,結束后)
classNames={{
enter: "animate__animated",
enterActive: "animate__fadeIn",
exit: "animate__animated",
exitActive: "animate__fadeOut",
}}
具體的我們可以進行參考animate官網網址:Animate.css | A cross-browser library of CSS animations.
原文鏈接:https://blog.csdn.net/qq_60976312/article/details/127531017
相關推薦
- 2022-04-27 VSCode?IDE?配置環境過程解析_C 語言
- 2023-07-08 el-table-column重構expand的樣式
- 2022-08-28 Python代碼中引用已經寫好的模塊、方法的兩種方式_python
- 2022-02-12 使用background-attachment實現視差滾動、水波
- 2022-11-09 Python有序容器的?sort?方法詳解_python
- 2022-11-05 Golang操作命令行的幾種方式總結_Golang
- 2022-12-06 Android?LayerDrawable超詳細講解_Android
- 2022-07-06 如何在React項目中優雅的使用對話框_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支