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
基础用法
组件安装次数
可用性
内置组件数
prefix 前缀(如货币符号)
separator 千分位分隔符
from 起始值
尺寸:SM / MD / LG / XL
complete 事件
变体(仅文字颜色)
Installation
pnpm dlx brutx-vue@latest add counterUsage
<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
<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. accent is a bright yellow that has low contrast on light backgrounds; use it on dark or colored backgrounds.
| Variant | Description |
|---|---|
default | Standard foreground text |
primary | Primary (coral) text |
accent | Accent (yellow) text |
success | Success (green) text |
danger | Destructive (red) text |
<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
| Size | Description |
|---|---|
sm | Small font size |
md | Default font size |
lg | Large font size |
xl | Extra large font size |
Programmatic Control
Counter exposes the following methods via defineExpose, allowing parent components to call them through ref:
<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>API Reference
| Name | Type | Default | Description |
|---|---|---|---|
toRequired | number | — | The required target value for the count-up animation. |
DetailsDeclared typenumber Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
from | number | undefined | 0 | The starting value; play() also restarts from this value. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
duration | number | undefined | 2000 | Animation duration in milliseconds; non-positive values use the component default duration. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
decimals | number | undefined | 0 | The number of displayed decimal places; values are formatted to this precision. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
decimalSeparator | string | undefined | "." | The separator displayed between the integer and fractional parts. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
prefix | string | undefined | "" | Text displayed before the number, such as a currency symbol. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
suffix | string | undefined | "" | Text displayed after the number, such as a percent sign or plus sign. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
prefixComponent | Component | undefined | undefined | A display component used instead of the text prefix. It is mounted again for layout measurement, so avoid internal state and side effects. |
DetailsDeclared typeComponent | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
suffixComponent | Component | undefined | undefined | A display component used instead of the text suffix. It is mounted again for layout measurement, so avoid internal state and side effects. |
DetailsDeclared typeComponent | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
animatePrefix | boolean | undefined | true | Whether to render prefixComponent when provided; when false, the prefix text is shown. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
animateSuffix | boolean | undefined | true | Whether to render suffixComponent when provided; when false, the suffix text is shown. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
separator | string | undefined | "," | The thousands separator. An empty string disables digit grouping while retaining decimalSeparator. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
easing | "ease-out" | "linear" | "ease-in-out" | undefined | "ease-out" | The easing function applied to animation progress. |
DetailsDeclared type"ease-out" | "linear" | "ease-in-out" | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
autoStart | boolean | undefined | true | Starts the animation on mount. When false, the value stays at from and to/from changes do not auto-play. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
variant | "accent" | "danger" | "default" | "primary" | "success" | undefined | "default" | The number text color variant; it does not change the card background. |
DetailsDeclared typeNonNullable<"default" | "primary" | "success" | "danger" | "accent" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
size | "lg" | "md" | "sm" | "xl" | undefined | "md" | The preset font size for the number. |
DetailsDeclared typeNonNullable<"lg" | "sm" | "md" | "xl" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
title | string | undefined | undefined | Optional heading text. Providing it enables container layout; title-slot content takes precedence. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
card | boolean | undefined | false | Wraps the value in a card container with border, padding, and shadow. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
valueStyle | CSSProperties | undefined | undefined | Inline styles applied to the wrapper around the value. |
DetailsDeclared typeCSSProperties | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
class | string | undefined | undefined | Classes added to the outer container in container mode, or to the number element when container mode is off. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/counter/Counter.vue | |||
| Name | Signature or type | Description |
|---|---|---|
@complete | [] | Emitted when the animation reaches to; it is also emitted when reduced-motion preferences cause the value to jump directly to the target. |
DetailsDeclared type[] Sourcepackages/ui/src/components/counter/Counter.vue | ||
| Name | Signature or type | Description |
|---|---|---|
#title | {} | Replaces the title prop content; providing this slot also enables container layout. |
DetailsDeclared type{}Sourcepackages/ui/src/components/counter/Counter.vue | ||
| Name | Signature or type | Description |
|---|---|---|
play | () => void | Cancels the current animation and restarts from from; under reduced-motion preferences it jumps directly to the target. |
DetailsDeclared type() => void Sourcepackages/ui/src/components/counter/Counter.vue | ||
stop | () => void | Cancels the current animation and stops further value updates. |
DetailsDeclared type() => void Sourcepackages/ui/src/components/counter/Counter.vue | ||
Accessibility
- ARIA Attributes: Number content is accessible to screen readers