Create fancy enter/leave animations in page elements, when switching between vue-router views.
npm i gist:bd502b019ba91f8f70b3a8f5cbb6f4d8
Install VueRouterAnimations plugin, passing the router object as a parameter.
import router from './router';
import VueRouterAnimations from 'vue-router-animations';
app.use(VueRouterAnimations, { router });
Import default animations or create your own. Animations are a pair of -enter and -leave steps.
import 'vue-router-animations/animations/fade.css;
@keyframes my-custom-enter {
from {
opacity: 0;
transform: rotate(180deg) translateX(100%) scale(0.5);
}
to {
opacity: 1;
transform: none;
}
}
@keyframes my-custom-leave {
from {
opacity: 1;
transform: none;
}
to {
opacity: 0;
transform: rotate(360deg) translate(-100%, 50%) scale(1.5);
}
}
Assign an animation to any element by defining custom data attributes or a vue directive instead. Other parameters such as duration and delay, both for enter and leave steps, can be defined using argument, modifiers and value or using a deep nested object as value.
<div v-routeranimation[:animation][.duration]="delay" />
<div v-routeranimation="options" />
Durations object must be defined at plugin config to allow using modifiers as duration shorthand.
app.use(VueRouterAnimations, {
router,
durations: { slow: '1s', fast: '0.25s' },
})
<div v-routeranimation:fade.slow />
<!-- Use my-custom animation -->
<img v-routeranimation:my-custom src="path.js">
<!-- Use fade animation with 2s delay -->
<img v-routeranimation:fade="'2s'" src="path.jpg">
<!-- Use different animations for enter/leave steps -->
<img v-routeranimation="{ enter: 'fade', leave: 'my-custom' }" src="path.jpg">
<!-- Advanced configuration -->
<img
src="path.jpg"
v-routeranimation="{
enter: 'fade',
duration: '1s',
delay: `${Math.random() * 5}s`
leave: {
animation: 'my-custom',
duration: '2s',
delay: '0s',
},
}">