Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js transition on data change

Tags:

vue.js

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

like image 415
dfilkovi Avatar asked Mar 27 '17 15:03

dfilkovi


People also ask

How do I watch data changes on vue instance?

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.

How do you use the vue transition?

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.

How do I transfer data from one component to another vue?

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!

What is Transition Group vue?

<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.


1 Answers

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;
    }
}
like image 111
Egor Stambakio Avatar answered Sep 18 '22 11:09

Egor Stambakio