count.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <div style="display: inline-block;">
  3. <i v-for="(i,k) in displayValue" :style="{background:((bg && i!=',')?'#142137':''),marginRight:((bg && i!=',')?'11px':''),width:((bg && i!=',')?'62px':'')}">{{i}}</i>
  4. </div>
  5. </template>
  6. <script>
  7. import { requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame.js'
  8. export default {
  9. props: {
  10. startVal: {
  11. type: Number,
  12. required: false,
  13. default: 0
  14. },
  15. endVal: {
  16. type: Number,
  17. required: false,
  18. default: 2017
  19. },
  20. duration: {
  21. type: Number,
  22. required: false,
  23. default: 2000
  24. },
  25. autoplay: {
  26. type: Boolean,
  27. required: false,
  28. default: true
  29. },
  30. decimals: {
  31. type: Number,
  32. required: false,
  33. default: 0,
  34. validator(value) {
  35. return value >= 0
  36. }
  37. },
  38. decimal: {
  39. type: String,
  40. required: false,
  41. default: '.'
  42. },
  43. separator: {
  44. type: String,
  45. required: false,
  46. default: ','
  47. },
  48. prefix: {
  49. type: String,
  50. required: false,
  51. default: ''
  52. },
  53. suffix: {
  54. type: String,
  55. required: false,
  56. default: ''
  57. },
  58. useEasing: {
  59. type: Boolean,
  60. required: false,
  61. default: true
  62. },
  63. easingFn: {
  64. type: Function,
  65. default(t, b, c, d) {
  66. return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  67. }
  68. },
  69. bg:{
  70. type:Boolean,
  71. default:false
  72. }
  73. },
  74. data() {
  75. return {
  76. localStartVal: this.startVal,
  77. displayValue: this.formatNumber(this.startVal),
  78. printVal: null,
  79. paused: false,
  80. localDuration: this.duration,
  81. startTime: null,
  82. timestamp: null,
  83. remaining: null,
  84. rAF: null
  85. };
  86. },
  87. computed: {
  88. countDown() {
  89. return this.startVal > this.endVal
  90. }
  91. },
  92. watch: {
  93. startVal() {
  94. if (this.autoplay) {
  95. this.start();
  96. }
  97. },
  98. endVal() {
  99. if (this.autoplay) {
  100. this.start();
  101. }
  102. }
  103. },
  104. mounted() {
  105. if (this.autoplay) {
  106. this.start();
  107. }
  108. this.$emit('mountedCallback')
  109. },
  110. methods: {
  111. start() {
  112. this.localStartVal = this.startVal;
  113. this.startTime = null;
  114. this.localDuration = this.duration;
  115. this.paused = false;
  116. this.rAF = requestAnimationFrame(this.count);
  117. },
  118. pauseResume() {
  119. if (this.paused) {
  120. this.resume();
  121. this.paused = false;
  122. } else {
  123. this.pause();
  124. this.paused = true;
  125. }
  126. },
  127. pause() {
  128. cancelAnimationFrame(this.rAF);
  129. },
  130. resume() {
  131. this.startTime = null;
  132. this.localDuration = +this.remaining;
  133. this.localStartVal = +this.printVal;
  134. requestAnimationFrame(this.count);
  135. },
  136. reset() {
  137. this.startTime = null;
  138. cancelAnimationFrame(this.rAF);
  139. this.displayValue = this.formatNumber(this.startVal);
  140. },
  141. count(timestamp) {
  142. if (!this.startTime) this.startTime = timestamp;
  143. this.timestamp = timestamp;
  144. const progress = timestamp - this.startTime;
  145. this.remaining = this.localDuration - progress;
  146. if (this.useEasing) {
  147. if (this.countDown) {
  148. this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration)
  149. } else {
  150. this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration);
  151. }
  152. } else {
  153. if (this.countDown) {
  154. this.printVal = this.localStartVal - ((this.localStartVal - this.endVal) * (progress / this.localDuration));
  155. } else {
  156. this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration);
  157. }
  158. }
  159. if (this.countDown) {
  160. this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal;
  161. } else {
  162. this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal;
  163. }
  164. this.displayValue = this.formatNumber(this.printVal)
  165. if (progress < this.localDuration) {
  166. this.rAF = requestAnimationFrame(this.count);
  167. } else {
  168. this.$emit('callback');
  169. }
  170. },
  171. isNumber(val) {
  172. return !isNaN(parseFloat(val))
  173. },
  174. formatNumber(num) {
  175. num = num.toFixed(this.decimals);
  176. num += '';
  177. const x = num.split('.');
  178. let x1 = x[0];
  179. const x2 = x.length > 1 ? this.decimal + x[1] : '';
  180. const rgx = /(\d+)(\d{3})/;
  181. if (this.separator && !this.isNumber(this.separator)) {
  182. while (rgx.test(x1)) {
  183. x1 = x1.replace(rgx, '$1' + this.separator + '$2');
  184. }
  185. }
  186. return this.prefix + x1 + x2 + this.suffix;
  187. }
  188. },
  189. destroyed() {
  190. cancelAnimationFrame(this.rAF)
  191. }
  192. };
  193. </script>
  194. <style>
  195. i{
  196. font-style: normal;
  197. display: inline-block;
  198. text-align: center;
  199. }
  200. </style>