Skip to content

CountTo 数字滚动

用于统计卡片、金额变化、数据大屏和指标增长动画等场景。

说明

该组件基于 useCountTo Hook 实现,支持缓动函数、千分位分隔和自定义前缀后缀。

平台差异说明

App(vue)App(nvue)H5小程序

基本使用

  • 通过 startVal 设置起始值,endVal 设置目标值。
  • 默认自动开始滚动动画。
html
<see-count-to :startVal="0" :endVal="2024" />

动画时长

  • 通过 duration 设置动画时长(毫秒),默认为 2000
html
<see-count-to :endVal="1000" :duration="3000" />
<see-count-to :endVal="1000" :duration="500" />

小数位数

  • 通过 decimals 设置小数位数。
  • 通过 decimal 设置小数点符号,默认为 .
html
<see-count-to :startVal="0" :endVal="99.99" :decimals="2" />
<see-count-to :startVal="0" :endVal="99.99" :decimals="2" decimal="," />

千分位分隔

  • 通过 separator 设置千分位分隔符,默认为 ,
html
<see-count-to :endVal="1000000" separator="," />
<see-count-to :endVal="1000000" separator="" />

前缀与后缀

  • 通过 prefix 设置前缀,suffix 设置后缀。
html
<see-count-to :endVal="9999" prefix="¥" />
<see-count-to :endVal="99.9" :decimals="1" suffix="%" />
<see-count-to :endVal="1000" prefix="累计 " suffix=" 件" />

缓动函数

  • 默认使用 easeOutExpo 缓动函数,产生先快后慢的效果。
  • 设置 useEasingfalse 可关闭缓动,使用线性动画。
html
<!-- 缓动动画(默认) -->
<see-count-to :endVal="10000" useEasing />

<!-- 线性动画 -->
<see-count-to :endVal="10000" :useEasing="false" />

手动控制

  • 设置 autoplayfalse 关闭自动开始。
  • 通过组件实例调用 start()pause()resume()reset() 方法。
vue
<see-count-to ref="counterRef" :endVal="10000" :autoplay="false" />
<button @tap="counterRef?.start()">开始</button>
<button @tap="counterRef?.pause()">暂停</button>
<button @tap="counterRef?.resume()">继续</button>
<button @tap="counterRef?.reset()">重置</button>

<script lang="ts" setup>
import { ref } from 'vue'
const counterRef = ref()
</script>
vue
<see-count-to ref="counterRef" :endVal="10000" :autoplay="false" />
<button @tap="$refs.counterRef.start()">开始</button>
<button @tap="$refs.counterRef.pause()">暂停</button>
<button @tap="$refs.counterRef.resume()">继续</button>
<button @tap="$refs.counterRef.reset()">重置</button>

<script>
export default {
  data() {
    return {}
  }
}
</script>

动态更新

  • 通过修改 endVal 可触发动态更新动画。
html
<see-count-to :endVal="endVal" />
<button @tap="endVal = Math.floor(Math.random() * 100000)">随机值</button>

插槽用法

  • 默认插槽暴露当前数值。
  • prefixsuffix 插槽可自定义前缀后缀。
html
<see-count-to :endVal="9999" prefix="¥" suffix="元">
  <template #default="{ value }">
    <text style="color: red">{{ value.toFixed(0) }}</text>
  </template>
</see-count-to>

自定义样式

  • 通过 color 设置文字颜色。
  • 通过 fontSize 设置字体大小。
  • 通过 fontWeight 设置字体粗细。
html
<see-count-to :endVal="2024" color="#ff0000" fontSize="48rpx" fontWeight="bold" />

API

Props

参数名说明类型默认值可选值平台差异
startVal起始值Number0任意数字-
endVal目标值Number0任意数字-
duration动画时长(毫秒)Number2000任意正整数-
autoplay是否自动开始动画Booleantruetruefalse-
decimals小数位数Number00–20-
decimal小数点符号String'.'任意字符串-
separator千分位分隔符String','任意字符串(可为空)-
prefix前缀文本String''任意字符串-
suffix后缀文本String''任意字符串-
useEasing是否使用缓动函数Booleantruetruefalse-
easingFn自定义缓动函数(t, b, c, d) => numberundefined(内置 easeOutExpo)任意缓动函数-
color文字颜色String'var(--see-text-color, #303133)'任意 CSS 颜色值-
fontSize字体大小String'32rpx'任意 CSS 字体大小值-
fontWeight字体粗细String / Number600任意 CSS 字体粗细值-

Events

事件名说明回调参数平台差异
onStart动画开始时触发-
onChange数值变化时触发value: number-
onFinish动画结束时触发-
onReset重置时触发-

Slots

插槽名说明作用域数据
default自定义数字显示{ value, displayValue }
prefix自定义前缀
suffix自定义后缀

Methods

通过 ref 获取组件实例后调用。

方法名说明参数
start开始动画
pause暂停动画
resume继续动画
reset重置到起始值
update更新目标值并重新动画value: number 新目标值

辽 ICP 备 2025070134 号