vue3.0切换动画原理
<template>
<router-view v-slot="{ Component }">
<transition :name="transitionName">
<component :is="Component" class="app-view"></component>
</transition>
</router-view>
</template>
<script lang='ts'>
import { reactive, toRefs, getCurrentInstance, watch } from "vue";
export default {
setup() {
const _this = getCurrentInstance()?.proxy as any;
const state = reactive({
transitionName: "",
});
watch(
() => _this.$route,
() => {
if (_this.$router.isPush) {
state.transitionName = "jump";
_this.$router.isPush = false;
} else if (_this.$router.isPush === false) {
state.transitionName = "back";
} else {
_this.$router.isPush = false;
state.transitionName = "";
}
}
);
return {
...toRefs(state),
};
},
};
</script>
<style lang="scss" scoped>
* {
margin: 0;
padding: 0;
}
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.jump-enter-active {
transform: translate(100%, 0);
}
.jump-enter-to {
transform: translate(0, 0);
}
.back-leave-active {
z-index: 5;
transform: translate(0, 0);
}
.back-leave-to {
transform: translate(100%, 0);
}
.app-view {
transition: transform 0.3s;
}
</style>
React切换动画
- 引入插件react-transition-group里的两个组件TransitionGroup和CSSTransition
- 引入react-router里的Switch组件
- 引入umi里withRouter的Api
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'
import './index.less'
const routerType = {
'POP': 'back',
'PUSH': 'in',
'REPLACE': 'in'
}
export default withRouter(({ location, children, history }) => {
return (
<TransitionGroup className='transition_wrapper' childFactory={(child) => (
React.cloneElement(child, { classNames: routerType[history.action] })
)}>
<CSSTransition key={location.pathname} appear timeout={300}>
{}
<Switch location={location}>{(children as any)?.props?.children}</Switch>
</CSSTransition>
</TransitionGroup>
)
})
.in-enter-active,
.in-exit-active {
transition: all .3s;
}
.in-enter-active {
transform: translate(0, 0) !important;
}
.in-enter {
z-index: 5 !important;
transform: translate(100%, 0);
}
.back-enter-active,
.back-exit-active {
transition: all .3s;
}
.back-exit-active {
transform: translate(100%, 0) !important;
}
.back-exit {
z-index: 5 !important;
transform: translate(0, 0);
}