¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <text |
| | | class="u-count-num" |
| | | :style="{ |
| | | fontSize: $u.addUnit(fontSize), |
| | | fontWeight: bold ? 'bold' : 'normal', |
| | | color: color |
| | | }" |
| | | >{{ displayValue }}</text> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * countTo æ°åæ»å¨ |
| | | * @description 该ç»ä»¶ä¸è¬ç¨äºéè¦æ»å¨æ°åå°æä¸ä¸ªå¼çåºæ¯ï¼ç®æ è¦æ±æ¯ä¸ä¸ªéå¢çå¼ã |
| | | * @tutorial https://www.uviewui.com/components/countTo.html |
| | | * @property {String | Number} startVal å¼å§çæ°å¼ï¼é»è®¤ä»0å¢é¿å°æä¸ä¸ªæ°ï¼é»è®¤ 0 ï¼ |
| | | * @property {String | Number} endVal è¦æ»å¨çç®æ æ°å¼ï¼å¿
é¡» ï¼é»è®¤ 0 ï¼ |
| | | * @property {String | Number} duration æ»å¨å°ç®æ æ°å¼çå¨ç»æç»æ¶é´ï¼åä½ä¸ºæ¯«ç§ï¼msï¼ ï¼é»è®¤ 2000 ï¼ |
| | | * @property {Boolean} autoplay 设置æ°å¼åæ¯å¦èªå¨å¼å§æ»å¨ ï¼é»è®¤ true ï¼ |
| | | * @property {String | Number} decimals è¦æ¾ç¤ºçå°æ°ä½æ°ï¼è§å®ç½è¯´æï¼é»è®¤ 0 ï¼ |
| | | * @property {Boolean} useEasing æ»å¨ç»ææ¶ï¼æ¯å¦ç¼å¨ç»å°¾ï¼è§å®ç½è¯´æï¼é»è®¤ true ï¼ |
| | | * @property {String} decimal åè¿å¶åå² ï¼ é»è®¤ "." ï¼ |
| | | * @property {String} color åä½é¢è²ï¼ é»è®¤ '#606266' ) |
| | | * @property {String | Number} fontSize åä½å¤§å°ï¼åä½pxï¼ é»è®¤ 22 ï¼ |
| | | * @property {Boolean} bold å使¯å¦å ç²ï¼é»è®¤ false ï¼ |
| | | * @property {String} separator åä½åé符ï¼è§å®ç½è¯´æ |
| | | * @event {Function} end æ°å¼æ»å¨å°ç®æ 弿¶è§¦å |
| | | * @example <u-count-to ref="uCountTo" :end-val="endVal" :autoplay="autoplay"></u-count-to> |
| | | */ |
| | | export default { |
| | | name: 'u-count-to', |
| | | data() { |
| | | return { |
| | | localStartVal: this.startVal, |
| | | displayValue: this.formatNumber(this.startVal), |
| | | printVal: null, |
| | | paused: false, // æ¯å¦æå |
| | | localDuration: Number(this.duration), |
| | | startTime: null, // å¼å§çæ¶é´ |
| | | timestamp: null, // æ¶é´æ³ |
| | | remaining: null, // åççæ¶é´ |
| | | rAF: null, |
| | | lastTime: 0 // ä¸ä¸æ¬¡çæ¶é´ |
| | | }; |
| | | }, |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | computed: { |
| | | countDown() { |
| | | return this.startVal > this.endVal; |
| | | } |
| | | }, |
| | | watch: { |
| | | startVal() { |
| | | this.autoplay && this.start(); |
| | | }, |
| | | endVal() { |
| | | this.autoplay && this.start(); |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.autoplay && this.start(); |
| | | }, |
| | | methods: { |
| | | easingFn(t, b, c, d) { |
| | | return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b; |
| | | }, |
| | | requestAnimationFrame(callback) { |
| | | const currTime = new Date().getTime(); |
| | | // 为äºä½¿setTimteoutçå°½å¯è½çæ¥è¿æ¯ç§60å¸§çææ |
| | | const timeToCall = Math.max(0, 16 - (currTime - this.lastTime)); |
| | | const id = setTimeout(() => { |
| | | callback(currTime + timeToCall); |
| | | }, timeToCall); |
| | | this.lastTime = currTime + timeToCall; |
| | | return id; |
| | | }, |
| | | cancelAnimationFrame(id) { |
| | | clearTimeout(id); |
| | | }, |
| | | // å¼å§æ»å¨æ°å |
| | | start() { |
| | | this.localStartVal = this.startVal; |
| | | this.startTime = null; |
| | | this.localDuration = this.duration; |
| | | this.paused = false; |
| | | this.rAF = this.requestAnimationFrame(this.count); |
| | | }, |
| | | // æå®ç¶æï¼éæ°åå¼å§æ»å¨ï¼æè
æ»å¨ç¶æä¸ï¼æå |
| | | reStart() { |
| | | if (this.paused) { |
| | | this.resume(); |
| | | this.paused = false; |
| | | } else { |
| | | this.stop(); |
| | | this.paused = true; |
| | | } |
| | | }, |
| | | // æå |
| | | stop() { |
| | | this.cancelAnimationFrame(this.rAF); |
| | | }, |
| | | // éæ°å¼å§(æåçæ
åµä¸) |
| | | resume() { |
| | | if (!this.remaining) return |
| | | this.startTime = 0; |
| | | this.localDuration = this.remaining; |
| | | this.localStartVal = this.printVal; |
| | | this.requestAnimationFrame(this.count); |
| | | }, |
| | | // éç½® |
| | | reset() { |
| | | this.startTime = null; |
| | | this.cancelAnimationFrame(this.rAF); |
| | | this.displayValue = this.formatNumber(this.startVal); |
| | | }, |
| | | count(timestamp) { |
| | | if (!this.startTime) this.startTime = timestamp; |
| | | this.timestamp = timestamp; |
| | | const progress = timestamp - this.startTime; |
| | | this.remaining = this.localDuration - progress; |
| | | if (this.useEasing) { |
| | | if (this.countDown) { |
| | | this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration); |
| | | } else { |
| | | this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration); |
| | | } |
| | | } else { |
| | | if (this.countDown) { |
| | | this.printVal = this.localStartVal - (this.localStartVal - this.endVal) * (progress / this.localDuration); |
| | | } else { |
| | | this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration); |
| | | } |
| | | } |
| | | if (this.countDown) { |
| | | this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal; |
| | | } else { |
| | | this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal; |
| | | } |
| | | this.displayValue = this.formatNumber(this.printVal) || 0; |
| | | if (progress < this.localDuration) { |
| | | this.rAF = this.requestAnimationFrame(this.count); |
| | | } else { |
| | | this.$emit('end'); |
| | | } |
| | | }, |
| | | // 夿æ¯å¦æ°å |
| | | isNumber(val) { |
| | | return !isNaN(parseFloat(val)); |
| | | }, |
| | | formatNumber(num) { |
| | | // å°num转为Numberç±»åï¼å 为å
¶å¼å¯è½ä¸ºå符串æ°å¼ï¼è°ç¨toFixed伿¥é |
| | | num = Number(num); |
| | | num = num.toFixed(Number(this.decimals)); |
| | | num += ''; |
| | | const x = num.split('.'); |
| | | let x1 = x[0]; |
| | | const x2 = x.length > 1 ? this.decimal + x[1] : ''; |
| | | const rgx = /(\d+)(\d{3})/; |
| | | if (this.separator && !this.isNumber(this.separator)) { |
| | | while (rgx.test(x1)) { |
| | | x1 = x1.replace(rgx, '$1' + this.separator + '$2'); |
| | | } |
| | | } |
| | | return x1 + x2; |
| | | }, |
| | | destroyed() { |
| | | this.cancelAnimationFrame(this.rAF); |
| | | } |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | .u-count-num { |
| | | /* #ifndef APP-NVUE */ |
| | | display: inline-flex; |
| | | /* #endif */ |
| | | text-align: center; |
| | | } |
| | | </style> |