count-to.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <script>
  2. let lastTime = 0
  3. const prefixes = 'webkit moz ms o'.split(' ') // 各浏览器前缀
  4. let requestAnimationFrame
  5. let cancelAnimationFrame
  6. const isServer = typeof window === 'undefined'
  7. if (isServer) {
  8. requestAnimationFrame = function() {
  9. return
  10. }
  11. cancelAnimationFrame = function() {
  12. return
  13. }
  14. } else {
  15. requestAnimationFrame = window.requestAnimationFrame
  16. cancelAnimationFrame = window.cancelAnimationFrame
  17. let prefix
  18. // 通过遍历各浏览器前缀,来得到requestAnimationFrame和cancelAnimationFrame在当前浏览器的实现形式
  19. for (let i = 0; i < prefixes.length; i++) {
  20. if (requestAnimationFrame && cancelAnimationFrame) {
  21. break
  22. }
  23. prefix = prefixes[i]
  24. requestAnimationFrame = requestAnimationFrame || window[prefix + 'RequestAnimationFrame']
  25. cancelAnimationFrame = cancelAnimationFrame || window[prefix + 'CancelAnimationFrame'] || window[prefix + 'CancelRequestAnimationFrame']
  26. }
  27. // 如果当前浏览器不支持requestAnimationFrame和cancelAnimationFrame,则会退到setTimeout
  28. if (!requestAnimationFrame || !cancelAnimationFrame) {
  29. requestAnimationFrame = function(callback) {
  30. const currTime = new Date().getTime()
  31. // 为了使setTimteout的尽可能的接近每秒60帧的效果
  32. const timeToCall = Math.max(0, 16 - (currTime - lastTime))
  33. const id = window.setTimeout(() => {
  34. callback(currTime + timeToCall)
  35. }, timeToCall)
  36. lastTime = currTime + timeToCall
  37. return id
  38. }
  39. cancelAnimationFrame = function(id) {
  40. window.clearTimeout(id)
  41. }
  42. }
  43. }
  44. </script>
  45. <template id="count-to">
  46. <div>
  47. <span :style="countStyle">
  48. {{displayValue}}
  49. </span>
  50. </div>
  51. </template>
  52. <script>
  53. Vue.component('count-to', {
  54. name: "count-to",
  55. template: '#count-to',
  56. props: {
  57. startVal: {
  58. type: Number,
  59. required: false,
  60. default: 0
  61. },
  62. endVal: {
  63. type: Number,
  64. required: false,
  65. default: 2017
  66. },
  67. duration: {
  68. type: Number,
  69. required: false,
  70. default: 3000
  71. },
  72. autoplay: {
  73. type: Boolean,
  74. required: false,
  75. default: true
  76. },
  77. decimals: {
  78. type: Number,
  79. required: false,
  80. default: 0,
  81. validator(value) {
  82. return value >= 0
  83. }
  84. },
  85. decimal: {
  86. type: String,
  87. required: false,
  88. default: '.'
  89. },
  90. separator: {
  91. type: String,
  92. required: false,
  93. default: ','
  94. },
  95. prefix: {
  96. type: String,
  97. required: false,
  98. default: ''
  99. },
  100. suffix: {
  101. type: String,
  102. required: false,
  103. default: ''
  104. },
  105. useEasing: {
  106. type: Boolean,
  107. required: false,
  108. default: true
  109. },
  110. easingFn: {
  111. type: Function,
  112. default (t, b, c, d) {
  113. return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  114. }
  115. },
  116. countStyle: {
  117. type: Function,
  118. default () {
  119. return {};
  120. }
  121. }
  122. },
  123. data() {
  124. return {
  125. localStartVal: this.startVal,
  126. displayValue: this.formatNumber(this.startVal),
  127. printVal: null,
  128. paused: false,
  129. localDuration: this.duration,
  130. startTime: null,
  131. timestamp: null,
  132. remaining: null,
  133. rAF: null
  134. };
  135. },
  136. computed: {
  137. countDown() {
  138. return this.startVal > this.endVal
  139. }
  140. },
  141. watch: {
  142. startVal() {
  143. if (this.autoplay) {
  144. this.start();
  145. }
  146. },
  147. endVal() {
  148. if (this.autoplay) {
  149. this.start();
  150. }
  151. }
  152. },
  153. mounted() {
  154. if (this.autoplay) {
  155. this.start();
  156. }
  157. this.$emit('mountedCallback')
  158. },
  159. methods: {
  160. start() {
  161. this.localStartVal = this.startVal;
  162. this.startTime = null;
  163. this.localDuration = this.duration;
  164. this.paused = false;
  165. this.rAF = requestAnimationFrame(this.count);
  166. },
  167. pauseResume() {
  168. if (this.paused) {
  169. this.resume();
  170. this.paused = false;
  171. } else {
  172. this.pause();
  173. this.paused = true;
  174. }
  175. },
  176. pause() {
  177. cancelAnimationFrame(this.rAF);
  178. },
  179. resume() {
  180. this.startTime = null;
  181. this.localDuration = +this.remaining;
  182. this.localStartVal = +this.printVal;
  183. requestAnimationFrame(this.count);
  184. },
  185. reset() {
  186. this.startTime = null;
  187. cancelAnimationFrame(this.rAF);
  188. this.displayValue = this.formatNumber(this.startVal);
  189. },
  190. count(timestamp) {
  191. if (!this.startTime) this.startTime = timestamp;
  192. this.timestamp = timestamp;
  193. const progress = timestamp - this.startTime;
  194. this.remaining = this.localDuration - progress;
  195. if (this.useEasing) {
  196. if (this.countDown) {
  197. this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration)
  198. } else {
  199. this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration);
  200. }
  201. } else {
  202. if (this.countDown) {
  203. this.printVal = this.localStartVal - ((this.localStartVal - this.endVal) * (progress / this.localDuration));
  204. } else {
  205. this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration);
  206. }
  207. }
  208. if (this.countDown) {
  209. this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal;
  210. } else {
  211. this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal;
  212. }
  213. this.displayValue = this.formatNumber(this.printVal)
  214. if (progress < this.localDuration) {
  215. this.rAF = requestAnimationFrame(this.count);
  216. } else {
  217. this.$emit('callback');
  218. }
  219. },
  220. isNumber(val) {
  221. return !isNaN(parseFloat(val))
  222. },
  223. formatNumber(num) {
  224. num = num.toFixed(this.decimals);
  225. num += '';
  226. const x = num.split('.');
  227. let x1 = x[0];
  228. const x2 = x.length > 1 ? this.decimal + x[1] : '';
  229. const rgx = /(\d+)(\d{3})/;
  230. if (this.separator && !this.isNumber(this.separator)) {
  231. while (rgx.test(x1)) {
  232. x1 = x1.replace(rgx, '$1' + this.separator + '$2');
  233. }
  234. }
  235. return this.prefix + x1 + x2 + this.suffix;
  236. }
  237. },
  238. destroyed() {
  239. cancelAnimationFrame(this.rAF)
  240. }
  241. });
  242. </script>