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