123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <template>
- <view
- class="tn-count-num-class tn-count-num"
- :class="[fontColorClass]"
- :style="{
- fontSize: fontSizeStyle || '50rpx',
- fontWeight: bold ? 'bold' : 'normal',
- color: fontColorStyle || '#080808'
- }"
- >
- {{ displayValue }}
- </view>
- </template>
- <script>
- import componentsColorMixin from '../../libs/mixin/components_color.js'
- export default {
- name: 'tn-count-to',
- mixins: [componentsColorMixin],
- props: {
-
- startVal: {
- type: Number,
- default: 0
- },
-
- endVal: {
- type: Number,
- default: 0,
- required: true
- },
-
- autoplay: {
- type: Boolean,
- default: true
- },
-
- duration: {
- type: Number,
- default: 2000
- },
-
- useEasing: {
- type: Boolean,
- default: true
- },
-
- decimals: {
- type: Number,
- default: 0
- },
-
- decimalSeparator: {
- type: String,
- default: '.'
- },
-
-
- thousandthsSeparator: {
- type: String,
- default: ''
- },
-
- bold: {
- type: Boolean,
- default: false
- }
- },
- computed: {
- countDown() {
- return this.startVal > this.endVal
- }
- },
- data() {
- return {
- localStartVal: this.startVal,
- localDuration: this.duration,
-
- displayValue: this.formatNumber(this.startVal),
-
- printValue: null,
-
- paused: false,
-
- startTime: null,
-
- remainingTime: null,
-
- timestamp: null,
-
- lastTime: 0,
- rAF: null
- }
- },
- watch: {
- startVal() {
- this.autoplay && this.start()
- },
- endVal() {
- this.autoplay && this.start()
- }
- },
- mounted() {
- this.autoplay && this.start()
- },
- methods: {
-
- 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() {
- this.startTime = null
- this.localDuration = this.remainingTime
- this.localStartVal = this.printValue
- this.requestAnimationFrame(this.count)
- },
-
- reset() {
- this.startTime = null
- this.cnacelAnimationFrame(this.rAF)
- this.displayValue = this.formatNumber(this.startVal)
- },
-
- destroyed() {
- this.cancelAnimationFrame(this.rAF)
- },
-
- count(timestamp) {
- if (!this.startTime) this.startTime = timestamp
- this.timestamp = timestamp
- const progress = timestamp - this.startTime
- this.remainingTime = this.localDuration - progress
- if (this.useEasing) {
- if (this.countDown) {
- this.printValue = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration)
- } {
- this.printValue = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration)
- }
- } else {
- if (this.countDown) {
- this.printValue = this.localStartVal - (this.localStartVal - this.endVal) * (progress / this.localDuration)
- } else {
- this.printValue = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration)
- }
- }
- if (this.countDown) {
- this.printValue = this.printValue < this.endVal ? this.endVal : this.printValue
- } else {
- this.printValue = this.printValue > this.endVal ? this.endVal : this.printValue
- }
-
- this.displayValue = this.formatNumber(this.printValue)
- if (progress < this.localDuration) {
- this.rAF = this.requestAnimationFrame(this.count)
- } else {
- this.$emit('end')
- }
- },
-
- easingFn(t, b, c, d) {
- return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b
- },
-
- requestAnimationFrame(cb) {
- const currentTime = new Date().getTime()
-
- const timeToCall = Math.max(0, 16 - (currentTime - this.lastTime))
- const timerId = setTimeout(() => {
- cb && cb(currentTime + timeToCall)
- }, timeToCall)
- this.lastTime = currentTime + timeToCall
- return timerId
- },
-
- clearAnimationFrame(timerId) {
- clearTimeout(timerId)
- },
-
- formatNumber(number) {
- const reg = /(\d+)(\d{3})/
- number = Number(number)
- number = number.toFixed(Number(this.decimals))
- number += ''
- const numberArray = number.split('.')
- let num1 = numberArray[0]
- const num2 = numberArray.length > 1 ? this.decimalSeparator + numberArray[1] : ''
-
- if (this.thousandthsSeparator && !this.isNumber(this.thousandthsSeparator)) {
- while(reg.test(num1)) {
- num1 = num1.replace(reg, '$1' + this.thousandthsSeparator + '$2')
- }
- }
- return num1 + num2
- },
-
- isNumber(val) {
- return !isNaN(parseFloat(val))
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- .tn-count-num {
-
- display: inline-flex;
-
- text-align: center;
- line-height: 1;
- }
- </style>
|