Vue Numeric Count Animation
vue-count-to is a lightweight, dependency-free Vue component designed to animate the counting of numbers from a `startVal` to an `endVal` over a specified `duration`. It automatically determines whether to count up or down and supports customization of the easing function. Key features include the ability to set decimal places, separators, prefixes, and suffixes, and it offers `autoplay` functionality that triggers a re-count when `startVal` or `endVal` props change. The current stable version, as of the provided information, is `1.0.13`, indicating a mature and stable release. While a specific release cadence isn't explicitly mentioned in the documentation, its long-standing presence and continued use suggest it receives maintenance. Its primary differentiator is its simplicity and minimal footprint, making it ideal for straightforward numeric animations in Vue 2 applications, without the overhead of more complex animation libraries. It also supports Vue SSR.
Common errors
-
[Vue warn]: Unknown custom element: <countTo> - did you register the component correctly?
cause The `countTo` component was imported but not registered in the `components` option of the parent Vue component.fixAdd `countTo` to the `components` property within your Vue component's options: ```vue <script> import countTo from 'vue-count-to'; export default { components: { countTo }, // <-- Add this line // ... } </script> ``` -
TypeError: countTo is not a constructor (in browser console) or TypeError: Cannot read properties of undefined (reading 'start')
cause The `countTo` component was incorrectly imported using a named import syntax (`import { countTo } from 'vue-count-to';`) instead of a default import.fixChange the import statement to use a default import: `import countTo from 'vue-count-to';`
Warnings
- gotcha When `autoplay` is set to `true` (which is the default), the counting animation will automatically restart every time the `startVal` or `endVal` props change. This can lead to unexpected or repetitive animations if the values are frequently updated by other reactive data sources.
- gotcha Custom `easingFn` implementations must strictly adhere to the expected signature `(t, b, c, d)` where `t` is current time, `b` is start value, `c` is change in value, and `d` is total duration. Incorrect signatures, unexpected return values, or functions that do not correctly calculate the eased value will result in broken or non-smooth animations.
Install
-
npm install vue-count-to -
yarn add vue-count-to -
pnpm add vue-count-to
Imports
- countTo
import { countTo } from 'vue-count-to';import countTo from 'vue-count-to';
- Component Registration
components: { countTo }
Quickstart
<template>
<div>
<h1>Live Count:</h1>
<countTo :startVal='startVal' :endVal='endVal' :duration='3000' :decimals="2" separator="," prefix="$"></countTo>
<button @click="startCount">Start New Count</button>
</div>
</template>
<script>
import countTo from 'vue-count-to';
export default {
components: { countTo },
data () {
return {
startVal: 0,
endVal: 2025.56
}
},
methods: {
startCount() {
// Example of manually changing values to trigger re-count if autoplay is true
// Or manually start if autoplay is false via a ref (not shown in basic example)
this.startVal = Math.floor(Math.random() * 100);
this.endVal = Math.floor(Math.random() * 10000);
}
}
}
</script>