Skip to content

Counter

Pure native requestAnimationFrame-driven number easing animation with no third-party animation library dependency. Works great with Hero sections and Dashboard statistics displays.

Demo

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

Installation

pnpm dlx brutx-vue@latest add counter

Usage

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

const counterRef = ref()
</script>

<template>
    <!-- Basic usage -->
    <Counter :to="12800" suffix="+" size="lg" />

    <!-- Decimal point -->
    <Counter :to="99.9" :decimals="1" suffix="%" />

    <!-- Manual control -->
    <Counter ref="counterRef" :to="500" :auto-start="false" />
    <button @click="counterRef.play()">Start</button>
</template>

Custom Component Prefix/Suffix

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

<template>
    <!-- Use component as prefix -->
    <Counter :to="12800" :prefix-component="DollarSign" />

    <!-- Use text as suffix -->
    <Counter :to="99.9" :decimals="1" suffix="%" />

    <!-- Mixed: component prefix + text suffix -->
    <Counter :to="500" :prefix-component="DollarSign" suffix="万" :animate-suffix="false" />
</template>

Variants

variant only affects the number text color, making it easy to highlight statistics on different backgrounds.

VariantDescription
defaultStandard foreground text
primaryPrimary (coral) text
accentAccent (yellow) text
successSuccess (green) text
dangerDestructive (red) text
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>

Sizes

SizeDescription
smSmall font size
mdDefault font size
lgLarge font size
xlExtra large font size

Programmatic Control

Counter exposes the following methods via defineExpose, allowing parent components to call them through 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()">Start</button>
    <button @click="counterRef?.stop()">Stop</button>
</template>

Exposed API

Method/PropertyTypeDescription
play()() => voidRestart the animation from from
stop()() => voidImmediately stop the animation

Props

PropTypeDefaultDescription
tonumberTarget value (required)
fromnumber0Starting value
durationnumber2000Animation duration (in milliseconds)
decimalsnumber0Number of decimal places
decimalSeparatorstring'.'Decimal separator character
prefixstring''Number prefix (e.g., ¥ $)
suffixstring''Number suffix (e.g., + %)
prefixComponentComponentCustom prefix component
suffixComponentComponentCustom suffix component
animatePrefixbooleantrueWhether to show the custom prefix component (false shows text prefix)
animateSuffixbooleantrueWhether to show the custom suffix component (false shows text suffix)
separatorstring','Thousand separator, pass an empty string to disable
easing'linear' | 'ease-out' | 'ease-in-out''ease-out'Easing function
autoStartbooleantrueWhether to auto-play after mount
size'sm' | 'md' | 'lg' | 'xl''md'Font size preset
variant'default' | 'primary' | 'accent' | 'success' | 'danger''default'Text color variant (only affects text color, does not change background)
titlestringOptional label; enables container mode when provided
cardbooleanfalseWhether to enable card container styling
valueStyleCSSPropertiesInline style for the value area
classstringCustom CSS class

Events

EventPayloadDescription
completeFired when the animation finishes playing

Accessibility

  • ARIA Attributes: Number content is accessible to screen readers

Brute force builds.