I was trying to animated two divs using transition in Vuejs. Below is the code (jsfiddle) that I've used. But don't know why it's not working
https://jsfiddle.net/k6grqLh1/16/
vue
<div id="vue-instance">
<div>
<transition name="fade">
<div v-show="show">
<div class="box yellow"></div>
</div>
</transition>
<transition name="fade">
<div v-show="!show">
<div class="box blue"></div>
</div>
</transition>
<a href="#" @click="show = !show">Change</a>
</div>
</div>
css
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
opacity: 0
}
.box {
width:200px;height:200px;
}
.yellow{
background-color:yellow;
}
.blue{
background-color:blue;
}
js
var vm = new Vue({
el: '#vue-instance',
data: {
show: true
}
});
You have to add key in each of the div, besides adding vue 2.x in the fiddle, You need to make following changes:
Why from docs
HTML
Working fiddle: https://jsfiddle.net/k6grqLh1/21/
Edited
To make it more smooth, you can use Transition-Modes:
mode="out-in"
, which will make the current element transitions out first, then when complete, the new element transitions in. see below code:Fiddle: https://jsfiddle.net/k6grqLh1/22/
I would definitely suggest to use: https://github.com/asika32764/vue2-animate
Great lib with AnimateCSS ported to VueJS transitions. I am using that with every project and it works perfectly.
To use it with v-show with single element:
First of all.. you are importing Vue 1. If you import Vue 2 this html works
Then you should read the docs https://vuejs.org/v2/guide/transitions.html#Transitioning-Between-Elements if you want to see how transitions between elements are handled