umi实现路由动画

文章介绍了如何在Umi框架中添加路由动画,包括安装react-transition-group库,创建layout文件夹,编写index.tsx和index.less文件来定义动画效果,然后在pages文件夹创建页面,并在umirc.ts配置。最后通过npmstart启动项目,展示路由切换时的动画效果。
摘要由CSDN通过智能技术生成

一、安装react-transition-group

npm i react-transition-group

二、在src目录下创建layout文件夹,与pages同级

在这里插入图片描述

三、layout文件夹中创建index.tsx

在index.tsx中复制如下代码

import React, { useState, useEffect } from 'react'
import { TransitionGroup, CSSTransition } from 'react-transition-group'
import { history as Router, withRouter } from 'umi'
import { Switch } from 'react-router-dom'
import './index.less'

const routerType = {
    'POP': 'back',
    'PUSH': 'in',
    'REPLACE': 'in'
}

export default withRouter(({ location, children, history }) => {
    return (
        <TransitionGroup style={{ height: '100%' }} className='transition_wrapper' childFactory={(child) => (
            React.cloneElement(child, { classNames: routerType[history.action] })
        )}>
            <CSSTransition key={location.pathname} appear timeout={3000}>
                <Switch location={location}>{(children as any)?.props?.children}</Switch>
            </CSSTransition>
        </TransitionGroup>
    )
})

在index.less文件中复制如下代码

.dg-enter {
	transform: translate(100%, 0);
}

.dg-enter-active {
	transition: 3s;
	transform: translate(0);
}

.dg-enter-done {
	transform: translate(0);
}

.dg-exit-active {
	transition: 1s;
	transform: translate(-100%, 0);
}

.back-enter-active {
	transition: all 3s;
	transform: translate(0, 0) !important;
}

.back-enter-done {
	transform: translate(0, 0) !important;
}

.back-enter {
	z-index: 5 !important;
	transform: translate(-100%, 0);
}

.back-exit-active {
	opacity: 0;
	transition: all 3s;
	transform: translate(100%, 0) !important;
}

四、在pages文件夹中创建自己需要的页面

在这里插入图片描述
login文件夹中有index.tsx
在这里插入图片描述

wish文件夹中有index.tsx中点击可以跳转到登录页面

五、在umirc.ts文件中

在这里插入图片描述

六、npm start重新启动项目

七、演示效果

umi路由动画效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值