Skip to content

CountTo

Used for statistics cards, amount changes, data dashboards, and metric growth animations.

Note

This component is implemented based on the useCountTo Hook, supporting easing functions, thousand-separator formatting, and custom prefix/suffix.

Platform Differences

App (vue)App (nvue)H5Mini Program

Basic Usage

  • Set the start value via startVal and the target value via endVal.
  • The animation starts automatically by default.
html
<see-count-to :startVal="0" :endVal="2024" />

Animation Duration

  • Set the animation duration (in milliseconds) via duration, defaults to 2000.
html
<see-count-to :endVal="1000" :duration="3000" />
<see-count-to :endVal="1000" :duration="500" />

Decimal Places

  • Set decimal places via decimals.
  • Set the decimal point character via decimal, defaults to ..
html
<see-count-to :startVal="0" :endVal="99.99" :decimals="2" />
<see-count-to :startVal="0" :endVal="99.99" :decimals="2" decimal="," />

Thousand Separator

  • Set the thousand separator via separator, defaults to ,.
html
<see-count-to :endVal="1000000" separator="," />
<see-count-to :endVal="1000000" separator="" />

Prefix and Suffix

  • Set prefix via prefix and suffix via suffix.
html
<see-count-to :endVal="9999" prefix="$" />
<see-count-to :endVal="99.9" :decimals="1" suffix="%" />
<see-count-to :endVal="1000" prefix="Total " suffix=" items" />

Easing Function

  • Uses easeOutExpo easing function by default, producing a fast-then-slow effect.
  • Set useEasing to false to disable easing and use linear animation.
html
<!-- Eased animation (default) -->
<see-count-to :endVal="10000" useEasing />

<!-- Linear animation -->
<see-count-to :endVal="10000" :useEasing="false" />

Manual Control

  • Set autoplay to false to disable auto-start.
  • Control via component instance methods: start(), pause(), resume(), reset().
vue
<see-count-to ref="counterRef" :endVal="10000" :autoplay="false" />
<button @tap="counterRef?.start()">Start</button>
<button @tap="counterRef?.pause()">Pause</button>
<button @tap="counterRef?.resume()">Resume</button>
<button @tap="counterRef?.reset()">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()">Start</button>
<button @tap="$refs.counterRef.pause()">Pause</button>
<button @tap="$refs.counterRef.resume()">Resume</button>
<button @tap="$refs.counterRef.reset()">Reset</button>

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

Dynamic Update

  • Modifying endVal triggers a dynamic update animation.
html
<see-count-to :endVal="endVal" />
<button @tap="endVal = Math.floor(Math.random() * 100000)">Random</button>

Slot Usage

  • The default slot exposes the current value.
  • prefix and suffix slots allow custom prefix and suffix.
html
<see-count-to :endVal="9999" prefix="$" suffix=" USD">
  <template #default="{ value }">
    <text style="color: red">{{ value.toFixed(0) }}</text>
  </template>
</see-count-to>

Custom Styling

  • Set text color via color.
  • Set font size via fontSize.
  • Set font weight via fontWeight.
html
<see-count-to :endVal="2024" color="#ff0000" fontSize="48rpx" fontWeight="bold" />

API

Props

ParameterDescriptionTypeDefaultOptional ValuesPlatform Notes
startValStart valueNumber0Any number-
endValTarget valueNumber0Any number-
durationAnimation duration (ms)Number2000Any positive integer-
autoplayWhether to auto-start animationBooleantruetrue, false-
decimalsDecimal placesNumber00–20-
decimalDecimal point characterString'.'Any string-
separatorThousand separatorString','Any string (can be empty)-
prefixPrefix textString''Any string-
suffixSuffix textString''Any string-
useEasingWhether to use easing functionBooleantruetrue, false-
easingFnCustom easing function(t, b, c, d) => numberundefined (built-in easeOutExpo)Any easing function-
colorText colorString'var(--see-text-color, #303133)'Any CSS color value-
fontSizeFont sizeString'32rpx'Any CSS font size value-
fontWeightFont weightString / Number600Any CSS font weight value-

Events

EventDescriptionCallback ParametersPlatform Notes
onStartTriggered when animation startsNone-
onChangeTriggered on value changevalue: number-
onFinishTriggered when animation endsNone-
onResetTriggered on resetNone-

Slots

Slot NameDescriptionScope Data
defaultCustom number display{ value, displayValue }
prefixCustom prefixNone
suffixCustom suffixNone

Methods

Call via component instance obtained through ref.

MethodDescriptionParameters
startStart animationNone
pausePause animationNone
resumeResume animationNone
resetReset to start valueNone
updateUpdate target value and restartvalue: number New target

Liao ICP No. 2025070134