u-count-to.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <text
  3. class="u-count-num"
  4. :style="{
  5. fontSize: $u.addUnit(fontSize),
  6. fontWeight: bold ? 'bold' : 'normal',
  7. color: color
  8. }"
  9. >{{ displayValue }}</text>
  10. </template>
  11. <script>
  12. import props from './props.js';
  13. import mpMixin from '../../libs/mixin/mpMixin.js';
  14. import mixin from '../../libs/mixin/mixin.js';
  15. /**
  16. * countTo 数字滚动
  17. * @description 该组件一般用于需要滚动数字到某一个值的场景,目标要求是一个递增的值。
  18. * @tutorial https://ijry.github.io/uview-plus/components/countTo.html
  19. * @property {String | Number} startVal 开始的数值,默认从0增长到某一个数(默认 0 )
  20. * @property {String | Number} endVal 要滚动的目标数值,必须 (默认 0 )
  21. * @property {String | Number} duration 滚动到目标数值的动画持续时间,单位为毫秒(ms) (默认 2000 )
  22. * @property {Boolean} autoplay 设置数值后是否自动开始滚动 (默认 true )
  23. * @property {String | Number} decimals 要显示的小数位数,见官网说明(默认 0 )
  24. * @property {Boolean} useEasing 滚动结束时,是否缓动结尾,见官网说明(默认 true )
  25. * @property {String} decimal 十进制分割 ( 默认 "." )
  26. * @property {String} color 字体颜色( 默认 '#606266' )
  27. * @property {String | Number} fontSize 字体大小,单位px( 默认 22 )
  28. * @property {Boolean} bold 字体是否加粗(默认 false )
  29. * @property {String} separator 千位分隔符,见官网说明
  30. * @event {Function} end 数值滚动到目标值时触发
  31. * @example <u-count-to ref="uCountTo" :end-val="endVal" :autoplay="autoplay"></u-count-to>
  32. */
  33. export default {
  34. name: 'u-count-to',
  35. data() {
  36. return {
  37. localStartVal: this.startVal,
  38. displayValue: this.formatNumber(this.startVal),
  39. printVal: null,
  40. paused: false, // 是否暂停
  41. localDuration: Number(this.duration),
  42. startTime: null, // 开始的时间
  43. timestamp: null, // 时间戳
  44. remaining: null, // 停留的时间
  45. rAF: null,
  46. lastTime: 0 // 上一次的时间
  47. };
  48. },
  49. mixins: [mpMixin, mixin,props],
  50. computed: {
  51. countDown() {
  52. return this.startVal > this.endVal;
  53. }
  54. },
  55. watch: {
  56. startVal() {
  57. this.autoplay && this.start();
  58. },
  59. endVal() {
  60. this.autoplay && this.start();
  61. }
  62. },
  63. mounted() {
  64. this.autoplay && this.start();
  65. },
  66. methods: {
  67. easingFn(t, b, c, d) {
  68. return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b;
  69. },
  70. requestAnimationFrame(callback) {
  71. const currTime = new Date().getTime();
  72. // 为了使setTimteout的尽可能的接近每秒60帧的效果
  73. const timeToCall = Math.max(0, 16 - (currTime - this.lastTime));
  74. const id = setTimeout(() => {
  75. callback(currTime + timeToCall);
  76. }, timeToCall);
  77. this.lastTime = currTime + timeToCall;
  78. return id;
  79. },
  80. cancelAnimationFrame(id) {
  81. clearTimeout(id);
  82. },
  83. // 开始滚动数字
  84. start() {
  85. this.localStartVal = this.startVal;
  86. this.startTime = null;
  87. this.localDuration = this.duration;
  88. this.paused = false;
  89. this.rAF = this.requestAnimationFrame(this.count);
  90. },
  91. // 暂定状态,重新再开始滚动;或者滚动状态下,暂停
  92. reStart() {
  93. if (this.paused) {
  94. this.resume();
  95. this.paused = false;
  96. } else {
  97. this.stop();
  98. this.paused = true;
  99. }
  100. },
  101. // 暂停
  102. stop() {
  103. this.cancelAnimationFrame(this.rAF);
  104. },
  105. // 重新开始(暂停的情况下)
  106. resume() {
  107. if (!this.remaining) return
  108. this.startTime = 0;
  109. this.localDuration = this.remaining;
  110. this.localStartVal = this.printVal;
  111. this.requestAnimationFrame(this.count);
  112. },
  113. // 重置
  114. reset() {
  115. this.startTime = null;
  116. this.cancelAnimationFrame(this.rAF);
  117. this.displayValue = this.formatNumber(this.startVal);
  118. },
  119. count(timestamp) {
  120. if (!this.startTime) this.startTime = timestamp;
  121. this.timestamp = timestamp;
  122. const progress = timestamp - this.startTime;
  123. this.remaining = this.localDuration - progress;
  124. if (this.useEasing) {
  125. if (this.countDown) {
  126. this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration);
  127. } else {
  128. this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration);
  129. }
  130. } else {
  131. if (this.countDown) {
  132. this.printVal = this.localStartVal - (this.localStartVal - this.endVal) * (progress / this.localDuration);
  133. } else {
  134. this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration);
  135. }
  136. }
  137. if (this.countDown) {
  138. this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal;
  139. } else {
  140. this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal;
  141. }
  142. this.displayValue = this.formatNumber(this.printVal) || 0;
  143. if (progress < this.localDuration) {
  144. this.rAF = this.requestAnimationFrame(this.count);
  145. } else {
  146. this.$emit('end');
  147. }
  148. },
  149. // 判断是否数字
  150. isNumber(val) {
  151. return !isNaN(parseFloat(val));
  152. },
  153. formatNumber(num) {
  154. // 将num转为Number类型,因为其值可能为字符串数值,调用toFixed会报错
  155. num = Number(num);
  156. num = num.toFixed(Number(this.decimals));
  157. num += '';
  158. const x = num.split('.');
  159. let x1 = x[0];
  160. const x2 = x.length > 1 ? this.decimal + x[1] : '';
  161. const rgx = /(\d+)(\d{3})/;
  162. if (this.separator && !this.isNumber(this.separator)) {
  163. while (rgx.test(x1)) {
  164. x1 = x1.replace(rgx, '$1' + this.separator + '$2');
  165. }
  166. }
  167. return x1 + x2;
  168. },
  169. destroyed() {
  170. this.cancelAnimationFrame(this.rAF);
  171. }
  172. }
  173. };
  174. </script>
  175. <style lang="scss" scoped>
  176. @import "../../libs/css/components.scss";
  177. .u-count-num {
  178. /* #ifndef APP-NVUE */
  179. display: inline-flex;
  180. /* #endif */
  181. text-align: center;
  182. }
  183. </style>