Skip to content

Count To (useCountTo)

useCountTo is a Vue composable for number scrolling animations, supporting easing functions, thousand separators, and custom formatting.

It helps you easily handle: statistics cards, amount changes, data dashboards, metric growth animations, and more.

Source code: GitHub

Usage

vue
<template>
  <text>{{ counter.displayValue.value }}</text>
  <button @tap="counter.start()">Start</button>
  <button @tap="counter.pause()">Pause</button>
  <button @tap="counter.resume()">Resume</button>
  <button @tap="counter.reset()">Reset</button>
</template>

<script setup>
import { useCountTo } from 'see-u-ui'

const counter = useCountTo({
  startVal: 0,
  endVal: 10000,
  duration: 2000,
  onStart: () => console.log('Started'),
  onChange: (value) => console.log('Changed:', value),
  onFinish: () => console.log('Finished')
})
</script>
vue
<script setup>
import { useCountTo } from 'see-u-ui'

const counter = useCountTo({
  startVal: 0,
  endVal: 999999.99,
  duration: 3000,
  decimals: 2,
  decimal: '.',
  separator: ',',
  prefix: '$',
  suffix: ''
})
// Displays: $999,999.99
</script>
vue
<script setup>
import { useCountTo } from 'see-u-ui'

const counter = useCountTo({
  startVal: 0,
  endVal: 1000,
  duration: 2000
})

// Dynamically update to new target value
function updateTarget(newValue) {
  counter.update(newValue)
}
</script>

Easing Function

Uses easeOutExpo easing function by default, producing a fast-then-slow effect. Customize via easingFn.

typescript
// Easing function signature
type EasingFunction = (t: number, b: number, c: number, d: number) => number

// t: Current time
// b: Start value
// c: Change in value (endVal - startVal)
// d: Total duration

API

useCountTo(options)

Number scrolling management Hook

ParameterTypeDefaultDescription
options.startValnumber0Start value
options.endValnumber0Target value
options.durationnumber2000Animation duration (ms)
options.decimalsnumber0Decimal places
options.decimalstring'.'Decimal point character
options.separatorstring','Thousand separator
options.prefixstring''Prefix text
options.suffixstring''Suffix text
options.useEasingbooleantrueWhether to use easing function
options.easingFn(t, b, c, d) => numbereaseOutExpoCustom easing function
options.onStart() => voidundefinedAnimation start callback
options.onChange(value: number) => voidundefinedValue change callback
options.onFinish() => voidundefinedAnimation finish callback
options.onReset() => voidundefinedReset callback

Returns:

Property/MethodTypeDescription
currentValueRef<number>Current value
displayValueComputedRef<string>Formatted display value
isRunningRef<boolean>Whether running
start() => voidStart animation
pause() => voidPause animation
resume() => voidResume animation
reset() => voidReset to start value
update(value: number) => voidUpdate target and restart animation
cleanup() => voidCleanup animation

formatCountToValue(value, options?)

Format number display

ParameterTypeDefaultDescription
valuenumberInput value
optionsobject{}Format config
options.decimalsnumber0Decimal places
options.decimalstring'.'Decimal point
options.separatorstring','Thousand separator
options.prefixstring''Prefix
options.suffixstring''Suffix

Returns: string


easeOutExpo(t, b, c, d)

Built-in easing function (exponential decay)

ParameterTypeDescription
tnumberCurrent time
bnumberStart value
cnumberChange in value
dnumberTotal duration

Returns: number

Liao ICP No. 2025070134