Skip to content

xtf-count-to

组件说明

xtf-count-to 是数字动画组件,用于数值从起始值到目标值的动画过渡。支持缓动函数、千分位格式化、小数位、前后缀和自动播放,适用于数据大屏、统计卡片和金额展示等场景。


基础用法

1. 最简示例

通过 end 设置目标值,默认从 0 动画到目标值:

vue
<template>
  <xtf-count-to :end="8888" :duration="2000" />
</template>

2. 前后缀与格式化

通过 prefix / suffix 设置前后缀,通过 decimals 设置小数位,通过 separator 设置千分位分隔符:

vue
<template>
  <view style="display: flex; gap: 32rpx; flex-wrap: wrap">
    <view>
      <xtf-count-to :end="12899.5" prefix="¥" :decimals="2" :duration="1500" />
    </view>
    <view>
      <xtf-count-to :end="98.6" suffix="%" :decimals="1" :duration="1200" />
    </view>
    <view>
      <xtf-count-to :end="1000000" separator="," :duration="2000" />
    </view>
  </view>
</template>

3. 缓动函数

通过 easing 切换缓动函数,通过 useEasing 关闭缓动:

vue
<template>
  <view style="display: flex; flex-direction: column; gap: 16rpx">
    <view>
      <xtf-text level="caption" color="secondary">easeOutExpo(默认)</xtf-text>
      <xtf-count-to :end="5000" :duration="2000" use-easing easing="easeOutExpo" />
    </view>
    <view>
      <xtf-text level="caption" color="secondary">linear</xtf-text>
      <xtf-count-to :end="5000" :duration="2000" use-easing easing="linear" />
    </view>
    <view>
      <xtf-text level="caption" color="secondary">无缓动</xtf-text>
      <xtf-count-to :end="5000" :duration="2000" :use-easing="false" />
    </view>
  </view>
</template>

4. 手动控制

通过 autoplay 关闭自动播放,通过方法手动触发:

vue
<template>
  <view>
    <xtf-count-to ref="countTo" :end="9999" :duration="2000" :autoplay="false" />
    <xtf-button-group :gap="12" style="margin-top: 16rpx">
      <xtf-button label="开始" size="sm" @click="$refs.countTo.start()" />
      <xtf-button label="停止" size="sm" type="light" @click="$refs.countTo.stop()" />
      <xtf-button
        label="重置"
        size="sm"
        type="light"
        theme="danger"
        @click="$refs.countTo.reset()"
      />
    </xtf-button-group>
  </view>
</template>

全部属性

属性类型默认值作用描述适用范围
endNumber0目标数值目标值
startNumber0起始数值起始值
durationNumber | String2000动画时长(ms)动画速度
decimalsNumber0小数位数小数位
separatorString','千分位分隔符,空字符串不显示千分位
decimalString'.'小数点符号小数点
prefixString''前缀文字前缀
suffixString''后缀文字后缀
autoplayBooleantrue是否自动播放自动播放
useEasingBooleantrue是否使用缓动函数缓动开关
easingString'easeOutExpo'缓动函数:'easeOutExpo' / 'easeOutCubic' / 'easeOutQuart' / 'easeInOutCubic' / 'linear'缓动类型
colorString''文字颜色自定义颜色
sizeString | Number''字体大小(rpx)字体大小
boldBooleanfalse是否加粗加粗
alignString'left'对齐方式:'left' / 'center' / 'right'对齐
lineHeightString | Number''行高(rpx)行高
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
mounted组件挂载完成时触发--
finished动画播放完成时触发(value: Number)最终目标值
change动画数值变更时触发(value: Number)当前数值

事件使用示例

vue
<template>
  <xtf-count-to
    :end="120"
    :duration="600"
    @mounted="onMounted"
    @change="onChange"
    @finished="onFinished"
  />
</template>

<script>
export default {
  methods: {
    onMounted() {
      console.log('计数器已挂载')
    },
    onChange(value) {
      console.log('当前值', value)
    },
    onFinished(value) {
      uni.showToast({ title: '完成:' + value, icon: 'none' })
    }
  }
}
</script>

方法

方法名参数返回值说明
start--开始动画
stop--停止动画并保留当前值
reset--重置到起始值
update(endValue)-更新目标值并重新动画

主题说明

  • 文字颜色通过 color 属性控制,支持主题变量
  • 字体大小通过 size 属性控制
  • 加粗通过 bold 属性控制

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed