tm-flop.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view class="tm-flop vertical-align-middle d-inline-block ">
  3. <slot name="default" :value="displayValue">{{ displayValue }}</slot>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * 数字翻牌
  9. * @property {Number} startVal = [] 0,起始值
  10. * @property {Number} endVal = [] 0,最终值
  11. * @property {Number} duration = [] 3000,从起始值到结束值数字变动的时间
  12. * @property {Boolean} autoplay = [] true,是否自动播放
  13. * @property {Number} decimals = [] 0,保留的小数位数
  14. * @property {String} decimal = [] '.',小数点分割符号
  15. * @property {String} separator = [] ',',上了三位数分割的符号
  16. * @property {String} prefix = [] '',前缀
  17. * @property {String} suffix = [] '',后缀
  18. * @property {Boolean} isFrequent = [] false,是否隔一段时间数字跳动,这里的跳动是隔一段时间设置初始值
  19. * @property {Number} frequentTime = [] 5000,跳动间隔时间
  20. * 此库移植自:https://github.com/sitonlotus/vue-digital-flop
  21. */
  22. import tmTranslate from '@/tm-vuetify/components/tm-translate/tm-translate.vue';
  23. import { requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame';
  24. export default {
  25. name: 'tm-flop',
  26. components: {
  27. tmTranslate
  28. },
  29. props: {
  30. /**
  31. * @description 起始值
  32. */
  33. startVal: {
  34. type: Number,
  35. required: false,
  36. default: 0
  37. },
  38. /**
  39. * @description 最终值
  40. */
  41. endVal: {
  42. type: Number,
  43. required: false,
  44. default: 2021
  45. },
  46. /**
  47. * @description 从起始值到结束值数字变动的时间
  48. */
  49. duration: {
  50. type: Number,
  51. required: false,
  52. default: 3000
  53. },
  54. /**
  55. * @description 是否自动播放
  56. */
  57. autoplay: {
  58. type: Boolean,
  59. required: false,
  60. default: true
  61. },
  62. /**
  63. * @description 保留的小数位数
  64. */
  65. decimals: {
  66. type: Number,
  67. required: false,
  68. default: 0,
  69. validator(value) {
  70. return value >= 0;
  71. }
  72. },
  73. decimal: {
  74. type: String,
  75. required: false,
  76. default: '.'
  77. },
  78. /**
  79. * @description 三位三位的隔开效果
  80. */
  81. separator: {
  82. type: String,
  83. required: false,
  84. default: ','
  85. },
  86. /**
  87. * @description 前缀
  88. * @example '¥' 人民币前缀
  89. */
  90. prefix: {
  91. type: String,
  92. required: false,
  93. default: ''
  94. },
  95. /**
  96. * @description 后缀
  97. * @example
  98. */
  99. suffix: {
  100. type: String,
  101. required: false,
  102. default: ''
  103. },
  104. /**
  105. * @description 是否具有连贯性
  106. */
  107. useEasing: {
  108. type: Boolean,
  109. required: false,
  110. default: true
  111. },
  112. /**
  113. * @description 是否隔一段时间数字跳动,这里的跳动是隔一段时间设置初始值
  114. */
  115. isFrequent: {
  116. type: Boolean,
  117. required: false,
  118. default: false
  119. },
  120. /**
  121. * @description 跳动间隔时间
  122. */
  123. frequentTime: {
  124. type: Number,
  125. required: false,
  126. default: 5000
  127. }
  128. },
  129. data() {
  130. return {
  131. localStartVal: this.startVal,
  132. displayValue: this.formatNumber(this.startVal),
  133. printVal: null,
  134. paused: false,
  135. localDuration: this.duration,
  136. startTime: null,
  137. timestamp: null,
  138. remaining: null,
  139. rAF: null,
  140. timer: null
  141. };
  142. },
  143. computed: {
  144. countDown() {
  145. return this.startVal > this.endVal;
  146. }
  147. },
  148. watch: {
  149. startVal() {
  150. if (this.autoplay) {
  151. this.start();
  152. }
  153. },
  154. endVal() {
  155. if (this.autoplay) {
  156. this.start();
  157. }
  158. }
  159. },
  160. mounted() {
  161. if (this.autoplay) {
  162. this.start();
  163. }
  164. if (this.isFrequent && this.frequentTime) {
  165. this.timer = setInterval(() => {
  166. this.start(this.randomNum(0, this.endVal));
  167. }, this.frequentTime);
  168. }
  169. this.$emit('mountedCallback');
  170. },
  171. destroy(){
  172. this.destroyed();
  173. },
  174. methods: {
  175. easingFn(t = 0, b = 0, c = 0, d = 0) {
  176. let p = (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b;
  177. return p;
  178. },
  179. randomNum(a, b) {
  180. return Math.round(Math.random() * (b - a) + a);
  181. },
  182. start(startVal) {
  183. this.localStartVal = startVal || this.startVal;
  184. this.startTime = null;
  185. this.localDuration = this.duration;
  186. this.paused = false;
  187. this.rAF = requestAnimationFrame(this.count);
  188. },
  189. pauseResume() {
  190. if (this.paused) {
  191. this.resume();
  192. this.paused = false;
  193. } else {
  194. this.pause();
  195. this.paused = true;
  196. }
  197. },
  198. pause() {
  199. cancelAnimationFrame(this.rAF);
  200. },
  201. resume() {
  202. this.startTime = null;
  203. this.localDuration = +this.remaining;
  204. this.localStartVal = +this.printVal;
  205. requestAnimationFrame(this.count);
  206. },
  207. reset() {
  208. this.startTime = null;
  209. cancelAnimationFrame(this.rAF);
  210. this.displayValue = this.formatNumber(this.startVal);
  211. },
  212. count(timestamp) {
  213. if (!this.startTime) this.startTime = timestamp;
  214. this.timestamp = timestamp;
  215. const progress = timestamp - this.startTime;
  216. this.remaining = this.localDuration - progress;
  217. if (this.useEasing) {
  218. if (this.countDown) {
  219. this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration) || 0;
  220. } else {
  221. this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration);
  222. }
  223. } else {
  224. if (this.countDown) {
  225. this.printVal = this.localStartVal - (this.localStartVal - this.endVal) * (progress / this.localDuration);
  226. } else {
  227. this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration);
  228. }
  229. }
  230. if (this.countDown) {
  231. this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal;
  232. } else {
  233. this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal;
  234. }
  235. this.displayValue = this.formatNumber(this.printVal);
  236. if (progress < this.localDuration) {
  237. this.rAF = requestAnimationFrame(this.count);
  238. } else {
  239. this.$emit('callback');
  240. }
  241. },
  242. isNumber(val) {
  243. return !isNaN(parseFloat(val));
  244. },
  245. formatNumber(num) {
  246. num = num.toFixed(this.decimals);
  247. num += '';
  248. const x = num.split('.');
  249. let x1 = x[0];
  250. const x2 = x.length > 1 ? this.decimal + x[1] : '';
  251. const rgx = /(\d+)(\d{3})/;
  252. if (this.separator && !this.isNumber(this.separator)) {
  253. while (rgx.test(x1)) {
  254. x1 = x1.replace(rgx, '$1' + this.separator + '$2');
  255. }
  256. }
  257. return this.prefix + x1 + x2 + this.suffix;
  258. }
  259. },
  260. destroyed() {
  261. cancelAnimationFrame(this.rAF);
  262. this.timer && clearInterval(this.timer);
  263. }
  264. };
  265. </script>
  266. <style></style>