Vue.use(VueRouter);
const Home = {
template: `
<div class="home">
<h2>Home</h2>
<p>hello</p>
</div>
`
};
const Parent = {
data() {
return {
transitionName: "slide-left",
};
},
beforeRouteUpdate(to, from, next) {
this.transitionName = to.meta.animation;
next();
},
template: `
<div class="parent">
<h2>Parent</h2>
{{transitionName}}
<transition :name="transitionName">
<router-view class="child-view"></router-view>
</transition>
</div>
`
};
const Default = { template: '<div class="default">default</div>' };
const Foo = { template: '<div class="foo">foo</div>' };
const Bar = { template: '<div class="bar">bar</div>' };
const routes = [
{ path: "/", component: Home, td: 3, meta: {animation: 'slide-up'}, },
{
path: "/parent",
component: Parent,
td: 1,
meta: {animation: 'slide-down'},
children: [
{ path: "", component: Default, meta: {animation: 'slide-down'}},
{ path: "foo", component: Foo, meta: {animation: 'slide-right'}},
{ path: "bar", component: Bar, meta: {animation: 'slide-left'}}
]
}
]
const router = new VueRouter({
mode: "history",
routes
});
new Vue({
router,
template: `<div id="app">
<h1>Transitions</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/parent">/parent</router-link></li>
<li><router-link to="/parent/foo">/parent/foo</router-link></li>
<li><router-link to="/parent/bar">/parent/bar</router-link></li>
</ul>
<transition name="fade" mode="out-in">
<router-view class="view"></router-view>
</transition>
</div>`
}).$mount("#app");
.fade-enter-active, .fade-leave-active {
/* transition-property: opacity; */
/* transition-timing-function: ease; */
transition: all .5s cubic-bezier(.55,0,.1,1);
}
.fade-enter, .fade-leave-active {
opacity: 0
}
.view {
width: 50%;
margin: 0 auto;
}
.child-view {
position: absolute;
display: block;
top: 50%;
transition: all .5s cubic-bezier(.55,0,.1,1);
}
.slide-left-enter, .slide-right-leave-active {
opacity: 0;
-webkit-transform: translate(30px, 0);
transform: translate(30px, 0);
}
.slide-left-leave-active, .slide-right-enter {
opacity: 0;
-webkit-transform: translate(-30px, 0);
transform: translate(-30px, 0);
}
.slide-up-enter, .slide-down-leave-active {
opacity: 0;
-webkit-transform: translate(0, 30px);
transform: translate(0, 30px);
}
.slide-up-leave-active, .slide-down-enter {
opacity: 0;
-webkit-transform: translate(0, -30p0);
transform: translate(0, -30px);
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.3.0/vue-router.min.js"></script>
<div id="app"></div>