From d1448cb0ef10f358bb7bddb4e1ec268515e0b787 Mon Sep 17 00:00:00 2001 From: gaoluyang <2820782392@qq.com> Date: 星期二, 15 七月 2025 11:46:57 +0800 Subject: [PATCH] 项目初始化 --- uni_modules/uview-ui/components/u-count-to/u-count-to.vue | 184 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 184 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/components/u-count-to/u-count-to.vue b/uni_modules/uview-ui/components/u-count-to/u-count-to.vue new file mode 100644 index 0000000..417b732 --- /dev/null +++ b/uni_modules/uview-ui/components/u-count-to/u-count-to.vue @@ -0,0 +1,184 @@ +<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 婊氬姩鍒扮洰鏍囨暟鍊肩殑鍔ㄧ敾鎸佺画鏃堕棿锛屽崟浣嶄负姣锛坢s锛� 锛堥粯璁� 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 瀛椾綋澶у皬锛屽崟浣峱x锛� 榛樿 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(); + // 涓轰簡浣縮etTimteout鐨勫敖鍙兘鐨勬帴杩戞瘡绉�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) { + // 灏唍um杞负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> -- Gitblit v1.9.3