u-count-to.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. emits: ["end"],
  67. methods: {
  68. easingFn(t, b, c, d) {
  69. return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b;
  70. },
  71. requestAnimationFrame(callback) {
  72. const currTime = new Date().getTime();
  73. // 为了使setTimteout的尽可能的接近每秒60帧的效果
  74. const timeToCall = Math.max(0, 16 - (currTime - this.lastTime));
  75. const id = setTimeout(() => {
  76. callback(currTime + timeToCall);
  77. }, timeToCall);
  78. this.lastTime = currTime + timeToCall;
  79. return id;
  80. },
  81. cancelAnimationFrame(id) {
  82. clearTimeout(id);
  83. },
  84. // 开始滚动数字
  85. start() {
  86. this.localStartVal = this.startVal;
  87. this.startTime = null;
  88. this.localDuration = this.duration;
  89. this.paused = false;
  90. this.rAF = this.requestAnimationFrame(this.count);
  91. },
  92. // 暂定状态,重新再开始滚动;或者滚动状态下,暂停
  93. reStart() {
  94. if (this.paused) {
  95. this.resume();
  96. this.paused = false;
  97. } else {
  98. this.stop();
  99. this.paused = true;
  100. }
  101. },
  102. // 暂停
  103. stop() {
  104. this.cancelAnimationFrame(this.rAF);
  105. },
  106. // 重新开始(暂停的情况下)
  107. resume() {
  108. if (!this.remaining) return
  109. this.startTime = 0;
  110. this.localDuration = this.remaining;
  111. this.localStartVal = this.printVal;
  112. this.requestAnimationFrame(this.count);
  113. },
  114. // 重置
  115. reset() {
  116. this.startTime = null;
  117. this.cancelAnimationFrame(this.rAF);
  118. this.displayValue = this.formatNumber(this.startVal);
  119. },
  120. count(timestamp) {
  121. if (!this.startTime) this.startTime = timestamp;
  122. this.timestamp = timestamp;
  123. const progress = timestamp - this.startTime;
  124. this.remaining = this.localDuration - progress;
  125. if (this.useEasing) {
  126. if (this.countDown) {
  127. this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration);
  128. } else {
  129. this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration);
  130. }
  131. } else {
  132. if (this.countDown) {
  133. this.printVal = this.localStartVal - (this.localStartVal - this.endVal) * (progress / this.localDuration);
  134. } else {
  135. this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration);
  136. }
  137. }
  138. if (this.countDown) {
  139. this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal;
  140. } else {
  141. this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal;
  142. }
  143. this.displayValue = this.formatNumber(this.printVal) || 0;
  144. if (progress < this.localDuration) {
  145. this.rAF = this.requestAnimationFrame(this.count);
  146. } else {
  147. this.$emit('end');
  148. }
  149. },
  150. // 判断是否数字
  151. isNumber(val) {
  152. return !isNaN(parseFloat(val));
  153. },
  154. formatNumber(num) {
  155. // 将num转为Number类型,因为其值可能为字符串数值,调用toFixed会报错
  156. num = Number(num);
  157. num = num.toFixed(Number(this.decimals));
  158. num += '';
  159. const x = num.split('.');
  160. let x1 = x[0];
  161. const x2 = x.length > 1 ? this.decimal + x[1] : '';
  162. const rgx = /(\d+)(\d{3})/;
  163. if (this.separator && !this.isNumber(this.separator)) {
  164. while (rgx.test(x1)) {
  165. x1 = x1.replace(rgx, '$1' + this.separator + '$2');
  166. }
  167. }
  168. return x1 + x2;
  169. },
  170. destroyed() {
  171. this.cancelAnimationFrame(this.rAF);
  172. }
  173. }
  174. };
  175. </script>
  176. <style lang="scss" scoped>
  177. @import "../../libs/css/components.scss";
  178. .u-count-num {
  179. /* #ifndef APP-NVUE */
  180. display: inline-flex;
  181. /* #endif */
  182. text-align: center;
  183. }
  184. </style>