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