Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  192] [ 4]  / answers: 1 / hits: 24894  / 8 Years ago, sun, february 21, 2016, 12:00:00

I'm looking to animate number changes using VueJs.



For example I have:



{{ number }}


Then number changes from 0 to 100, I would like the element to count up to 100 rather than just jumping stright to it.



How would I do this without using any 3rd party (pure Js/VueJs) excluding VueJs?


More From » animation

 Answers
11

Got this working as a custom component: https://jsfiddle.net/5nobcLq0/5/


html


<body>
<input v-model="number">
<animated-number :number="number"></animated-number>
</body>

js


Vue.component('animated-number', {

template:"{{ displayNumber }}",
props: {'number': { default:0 }},

data () {
return {
displayNumber:0,
interval:false
}
},

ready () {
this.displayNumber = this.number ? this.number : 0;
},

watch: {
number () {
clearInterval(this.interval);

if(this.number == this.displayNumber) {
return;
}

this.interval = window.setInterval(() => {
if(this.displayNumber != this.number) {
var change = (this.number - this.displayNumber) / 10;
change = change >= 0 ? Math.ceil(change) : Math.floor(change);
this.displayNumber = this.displayNumber + change;
}
}, 20);
}
}
})

new Vue({
el:'body',
});

[#63243] Thursday, February 18, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;