gui-number-animate.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <text
  3. :class="customClass">{{numAnimate}}</text>
  4. </template>
  5. <script>
  6. export default{
  7. name : "gui-number-animate",
  8. props : {
  9. num : { type : Number, default : 0},
  10. stepNumber : { type : Number, default : 50 },
  11. timer : { type : Number, default : 1000 },
  12. keepInt : { type : Boolean, default : false },
  13. customClass : { type : Array, default : function(){
  14. return ['gui-primary-text', 'gui-text'];
  15. }
  16. }
  17. },
  18. data() {
  19. return {
  20. numAnimate : 0,
  21. intervalId : null
  22. }
  23. },
  24. created:function(){
  25. if(this.num != 0){this.run();}
  26. },
  27. watch:{
  28. num : function(val){
  29. if(this.intervalId != null){clearInterval(this.intervalId);}
  30. this.run();
  31. }
  32. },
  33. methods:{
  34. run : function(){
  35. let timer = this.timer / this.stepNumber;
  36. let step = Math.floor((this.num / this.stepNumber) * 100) / 100;
  37. this.intervalId = setInterval(() => {
  38. // 正值
  39. if(this.num >= 0){
  40. if(this.numAnimate >= this.num){
  41. this.numAnimate = this.num;
  42. clearInterval(this.intervalId);
  43. this.$emit('done');
  44. return;
  45. }
  46. }else{
  47. if(this.numAnimate <= this.num){
  48. this.numAnimate = this.num;
  49. clearInterval(this.intervalId);
  50. this.$emit('done');
  51. return;
  52. }
  53. }
  54. let res = this.numAnimate + step;
  55. this.numAnimate = this.keepInt ? parseInt(res) : Math.floor(res * 100) / 100;
  56. }, timer);
  57. }
  58. },
  59. emits : ['done']
  60. }
  61. </script>
  62. <style scoped>
  63. </style>