gui-number-animate.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <text :style="{
  3. fontSize:fontSize,
  4. fontWeight:fontWeight,
  5. color:color,
  6. lineHeight:lineHeight
  7. }">{{numAnimate}}</text>
  8. </template>
  9. <script>
  10. export default{
  11. name : "gui-number-animate",
  12. props : {
  13. num : { type : Number, default : 0 },
  14. stepNumber : { type : Number, default : 50 },
  15. timer : { type : Number, default : 1000 },
  16. keepInt : { type : Boolean, default : false },
  17. fontSize : { type : String, default : '28rpx' },
  18. color : { type : String, default : '#333333' },
  19. lineHeight : { type : String, default : '50rpx' },
  20. fontWeight : { type : String, default : '400' }
  21. },
  22. data() {
  23. return {
  24. numAnimate : 0,
  25. intervalId : null
  26. }
  27. },
  28. created:function(){
  29. let timer = this.timer / this.stepNumber;
  30. let step = Math.floor((this.num / this.stepNumber) * 100) / 100;
  31. this.intervalId = setInterval(() => {
  32. // 正值
  33. if(this.num >= 0){
  34. if(this.numAnimate >= this.num){
  35. this.numAnimate = this.num;
  36. clearInterval(this.intervalId);
  37. return;
  38. }
  39. }else{
  40. if(this.numAnimate <= this.num){
  41. this.numAnimate = this.num;
  42. clearInterval(this.intervalId);
  43. return;
  44. }
  45. }
  46. let res = this.numAnimate + step;
  47. this.numAnimate = this.keepInt ? parseInt(res) : Math.floor(res * 100) / 100;
  48. }, timer);
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. </style>