Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add CSS animations when updating a value with vue.js?

Tags:

vue.js

vuejs2

I am using vue.js to render an array into a list.

Each item in the list has a numeric value, and when that value changes I would like to use an animation.

Examples of animations:

  • Existing value fades out, new value fades in.
  • Yellow background behind value which fades out.
  • Text colour changes then fades back to original.

How can I do this?

HTML

<div id="app">
  <ul>
    <li v-for="user in users">
      {{ user.name }} = {{ user.value }}
    </li>
  </ul>

  <button v-on:click="users[0].value++">Change value</button>
</div>

JS

var app = new Vue({
  el: '#app',
  data:
  {
    users:
    [
      { name: 'Barbara Dwyer', value: 14 },
      { name: 'William B Hardigan', value: 10 }
    ]
  }
})

http://codepen.io/anon/pen/ryxjOE

like image 948
James Avatar asked Mar 01 '17 16:03

James


People also ask

How do you animate CSS in Vue?

Start the animation on click event. Start the animation onmouseover to the element. Start the animation on click event, with 500ms delay and 100ms animation duration. Start the animation on page load, with 5000ms animation duration and infinite iteration.

What is transition in Vue?

Vue offers two built-in components that can help work with transitions and animations in response to changing state: <Transition> for applying animations when an element or component is entering and leaving the DOM.

What CSS animation transitions can do?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.


2 Answers

You'd want to use :key along with a <transition>. Here's a very basic demo.

<transition name="slide-fade" mode="out-in">
  <div :key="value">
    {{ value }}
  </div>
</transition>

Then, as value changes the slide-fade animation will be used. An element with the old value will use the leave animation and the element with the new value will use the enter animation.

Here's a quick demo: https://jsfiddle.net/jx52bfpc/2/

like image 73
Bill Criswell Avatar answered Oct 17 '22 12:10

Bill Criswell


You can use a standard css animation with a key dependent on the changed value. In this way a new animation starts for the new element while the previous one is discarded.

new Vue({
  data: {
    value: 5,
  },
  methods: {
    add() {
      this.value++
    },
    subtract() {
      this.value--
    },
    keep() {
      this.value=this.value
    },
  },
}).$mount('#app')
.mycolor {
  background-color: green;
  animation: mymove 5s;
}
@keyframes mymove {
  from {
    background-color: red;
  }
  to {
    background-color: yellow;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="mycolor" :key="value">
    {{ value }}
  </div>
  <button @click="add">
   Add
  </button>
  <button @click="subtract">
   Subtract
  </button>
  <button @click="keep">
   Keep
  </button>
</div>
like image 21
raisercostin Avatar answered Oct 17 '22 11:10

raisercostin