Created
May 23, 2017 07:18
-
-
Save knubbbe/a054ac13d34c9899d52f03530d4d634a to your computer and use it in GitHub Desktop.
Vue animated number component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.component('animated-number', { | |
name: 'animated-number', | |
template: '<span :class="[classes]">{{ displayNumber }}</span>', | |
props: { | |
number: { | |
type: Number, | |
default: 0 | |
}, | |
speed: { | |
type: Number, | |
default: 10 | |
}, | |
classes: { | |
type: String, | |
default: '' | |
} | |
}, | |
data: function() { | |
return { | |
displayNumber: 0, | |
interval: false | |
}; | |
}, | |
ready:function(){ | |
this.displayNumber = this.number ? this.number : 0; | |
}, | |
watch: { | |
number: function(){ | |
clearInterval(this.interval); | |
if(this.number === this.displayNumber){ | |
return; | |
} | |
this.interval = window.setInterval(function(){ | |
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; | |
} | |
}.bind(this), this.speed); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment