Skip to content

Counter 数字滚动

纯原生 requestAnimationFrame 驱动的数字缓动动画,无第三方动画库依赖,配合 Hero 区块和 Dashboard 统计展示效果极佳。

预览

Preview

基础用法

0 +

组件安装次数

0.0 %

可用性

0

内置组件数

prefix 前缀(如货币符号)

$ 0 ¥ 0

separator 千分位分隔符

0 0 0

from 起始值

100 1,000

尺寸:SM / MD / LG / XL

0 SM
0 MD
0 LG
0 XL

complete 事件

0

变体(仅文字颜色)

0 default
0 primary
0 accent
0 success
0 danger

安装

pnpm dlx brutx-vue@latest add counter

用法

vue
<script setup>
import { Counter } from 'brutx-ui-vue'
import { ref } from 'vue'

const counterRef = ref()
</script>

<template>
    <!-- 基础用法 -->
    <Counter :to="12800" suffix="+" size="lg" />

    <!-- 小数点 -->
    <Counter :to="99.9" :decimals="1" suffix="%" />

    <!-- 手动控制 -->
    <Counter ref="counterRef" :to="500" :auto-start="false" />
    <button @click="counterRef.play()">开始</button>
</template>

自定义组件前缀/后缀

vue
<script setup>
import { Counter } from 'brutx-ui-vue'
import { DollarSign, Percent } from 'lucide-vue-next'
</script>

<template>
    <!-- 使用组件作为前缀 -->
    <Counter :to="12800" :prefix-component="DollarSign" />

    <!-- 使用文本作为后缀 -->
    <Counter :to="99.9" :decimals="1" suffix="%" />

    <!-- 混合使用:组件前缀 + 文本后缀 -->
    <Counter :to="500" :prefix-component="DollarSign" suffix="万" :animate-suffix="false" />
</template>

变体

variant 仅影响数字文字颜色,方便在不同背景上突出展示统计数值。

变体说明
default标准前景色文字
primaryPrimary(珊瑚色)文字
accentAccent(黄色)文字
successSuccess(绿色)文字
dangerDestructive(红色)文字
vue
<script setup>
import { Counter } from 'brutx-ui-vue'
</script>

<template>
    <div class="flex flex-wrap items-end gap-6">
        <Counter :to="128" variant="default" size="lg" />
        <Counter :to="128" variant="primary" size="lg" />
        <Counter :to="128" variant="accent" size="lg" />
        <Counter :to="128" variant="success" size="lg" />
        <Counter :to="128" variant="danger" size="lg" />
    </div>
</template>

尺寸

尺寸说明
sm小字号
md默认字号
lg大字号
xl超大字号

程序化控制

Counter 通过 defineExpose 暴露以下方法,允许父组件通过 ref 调用:

vue
<script setup>
import { ref } from 'vue'
import { Counter } from 'brutx-ui-vue'

const counterRef = ref()
</script>

<template>
    <Counter ref="counterRef" :to="500" :auto-start="false" />
    <button @click="counterRef?.play()">开始</button>
    <button @click="counterRef?.stop()">停止</button>
</template>

暴露的 API

方法/属性类型说明
play()() => voidfrom 重新开始播放动画
stop()() => void立即停止动画

Props

属性类型默认值说明
tonumber目标数值(必填)
fromnumber0起始数值
durationnumber2000动画时长(毫秒)
decimalsnumber0小数位数
decimalSeparatorstring'.'小数分隔符
prefixstring''数字前缀(如 ¥ $
suffixstring''数字后缀(如 + %
prefixComponentComponent自定义前缀组件
suffixComponentComponent自定义后缀组件
animatePrefixbooleantrue是否显示自定义前缀组件(false 时显示文本前缀)
animateSuffixbooleantrue是否显示自定义后缀组件(false 时显示文本后缀)
separatorstring','千位分隔符,传空字符串可禁用
easing'linear' | 'ease-out' | 'ease-in-out''ease-out'缓动函数
autoStartbooleantrue是否挂载后自动播放
size'sm' | 'md' | 'lg' | 'xl''md'字号预设
variant'default' | 'primary' | 'accent' | 'success' | 'danger''default'文字颜色变体(仅影响文字颜色,不改变背景)
titlestring可选标题,传入后启用容器模式
cardbooleanfalse是否启用卡片容器样式
valueStyleCSSProperties数值区域内联样式
classstring自定义样式类

事件

事件参数说明
complete动画播放完毕时触发

可访问性

  • ARIA 属性:数字内容对屏幕阅读器可访问

蛮力铸就。