u-count-to.vue 6.4 KB

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