In Vue documentation there is only example how to show or hide element but what if you want to show some transition based on value change. For example if new value is higher then old then show an arrow up for 3 seconds and if value is lower than old one show arrow down for 3 seconds. How can I achieve that?
Here is the jsfiddle: http://jsfiddle.net/d2hs7muq/2/
var data = {
number: 1
}
Vue.component('odd',
{
props: ['number', 'oldNumber'],
template: `<div>{{number}}
<transition name="change">
<div v-if="oldNumber && number > oldNumber"class="up">up</div>
<div v-if="oldNumber && number < oldNumber" class="down">down</div>
</transition>
</div>`,
watch:
{
number: function(number, oldNumber)
{
var me = this;
me.oldNumber = oldNumber;
}
}
});
var demo = new Vue({
el: '#demo',
data: data
})
As you can see css .change-enter-active gets triggered only on first change after that element stays in and no change is triggered at all
vue file we can watch for changes in data or props by using watch . For example, the below code will watch for a change in the data element pageData , and run a function according to the value it is changed to.
Vue Transition Example Just choose the element that you want to transition and wrap it in a <transition> component. For this example, we're creating a button that toggles a <p> element to render. And the corresponding <script> section. Now, we just have to add some styles to actually get the transitions working.
Using Props To Share Data From Parent To Child. VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!
<TransitionGroup> is a built-in component designed for animating the insertion, removal, and order change of elements or components that are rendered in a list.
for example (not tested):
in template:
<transition name="somewhat" appear>
<div class="up" v-if="bigger" key="up"></div>
<div class="down" v-else key="down"></div>
</transition>
in script section:
data: function() {
return: {
value: null,
bigger: false
}
}
computed: {
...mapGetters([
'getYourStoreValue'
])
},
watch: {
// value has changed
getYourStoreValue: function() {
let old = this.value;
if (this.getYourStoreValue > old) {
this.bigger = true;
} else {...etc...}
// save new value to 'value' data property
this.value = getYourStoreValue;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With