count.vue 6.6 KB

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