wd-count-to.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view :class="rootClass">
  3. <!-- 前缀插槽 -->
  4. <slot name="prefix">
  5. <wd-text :type="props.type" :color="props.color" :size="`${props.fontSize * 0.7}px`" :text="props.prefix"></wd-text>
  6. </slot>
  7. <!-- 默认文本插槽 -->
  8. <slot>
  9. <wd-text :type="props.type" :color="props.color" :size="`${props.fontSize}px`" :text="timeText"></wd-text>
  10. </slot>
  11. <!-- 后缀插槽 -->
  12. <slot name="suffix">
  13. <wd-text :type="props.type" :color="props.color" :size="`${props.fontSize * 0.7}px`" :text="props.suffix"></wd-text>
  14. </slot>
  15. </view>
  16. </template>
  17. <script lang="ts">
  18. export default {
  19. name: 'wd-count-to',
  20. options: {
  21. virtualHost: true,
  22. addGlobalClass: true,
  23. styleIsolation: 'shared'
  24. }
  25. }
  26. </script>
  27. <script lang="ts" setup>
  28. import wdText from '../wd-text/wd-text.vue'
  29. import { computed, watch, onMounted } from 'vue'
  30. import { countToProps } from './types'
  31. import { easingFn, isNumber } from '../common/util'
  32. import { useCountDown } from '../composables/useCountDown'
  33. import type { CountDownExpose } from '../wd-count-down/types'
  34. const props = defineProps(countToProps)
  35. const emit = defineEmits(['mounted', 'finish'])
  36. const { start, pause, reset, current } = useCountDown({
  37. time: props.duration,
  38. millisecond: true,
  39. onFinish: () => emit('finish')
  40. })
  41. // 计算根元素的类名
  42. const rootClass = computed(() => {
  43. return `wd-count-to ${props.customClass}`
  44. })
  45. const timeText = computed(() => {
  46. return parseFormat(current.value.total)
  47. })
  48. watch([() => props.startVal, () => props.endVal, () => props.duration], resetTime, { immediate: false })
  49. onMounted(() => {
  50. resetTime()
  51. emit('mounted')
  52. })
  53. // 重置动画
  54. function resetTime() {
  55. reset(props.duration)
  56. if (props.autoStart) {
  57. start()
  58. }
  59. }
  60. function parseFormat(remain: number) {
  61. const { startVal, endVal, duration, useEasing } = props
  62. const progress = duration - remain // 已经进行的时间
  63. const isPositive = startVal > endVal // 判断startVal是否大于endVal
  64. const progressRatio = progress / duration // 计算进度比例
  65. let currentVal: number
  66. if (useEasing) {
  67. // 使用缓动函数计算currentVal
  68. if (isPositive) {
  69. currentVal = startVal - easingFn(progress, 0, startVal - endVal, duration) || 0
  70. } else {
  71. currentVal = easingFn(progress, startVal, endVal - startVal, duration)
  72. }
  73. } else {
  74. // 不使用缓动函数时的计算方式
  75. if (isPositive) {
  76. currentVal = startVal - (startVal - endVal) * progressRatio
  77. } else {
  78. currentVal = startVal + (endVal - startVal) * progressRatio
  79. }
  80. }
  81. // 确保currentVal在startVal和endVal之间
  82. currentVal = isPositive ? Math.max(endVal, currentVal) : Math.min(endVal, currentVal)
  83. return formatNumber(currentVal)
  84. }
  85. // 格式化数字
  86. function formatNumber(num: any): string {
  87. if (typeof num !== 'number') {
  88. num = parseFloat(num)
  89. if (isNaN(num)) {
  90. return '0'
  91. }
  92. }
  93. num = num.toFixed(props.decimals)
  94. const parts = num.split('.')
  95. let integerPart = parts[0]
  96. const decimalPart = parts.length > 1 ? props.decimal + parts[1] : ''
  97. const rgx = /(\d+)(\d{3})/
  98. if (props.separator && !isNumber(props.separator)) {
  99. while (rgx.test(integerPart)) {
  100. integerPart = integerPart.replace(rgx, '$1' + props.separator + '$2')
  101. }
  102. }
  103. return integerPart + decimalPart
  104. }
  105. defineExpose<CountDownExpose>({ start, reset: resetTime, pause })
  106. </script>
  107. <style lang="scss" scoped>
  108. @import './index.scss';
  109. </style>