Skip to content

v-counter

Display animated number counter.

Since: 1.5.0

Usage

Basic

vue
<template>
  <span v-counter="1000">0</span>
</template>

With Options

vue
<template>
  <span v-counter="{
    value: 10000,
    duration: 3000,
    decimals: 2,
    useGrouping: true
  }">0</span>
</template>

With Formatter

vue
<template>
  <span v-counter="{
    value: targetValue,
    formatter: (v) => '$' + v.toFixed(2)
  }">0</span>
</template>

API

Options

OptionTypeDefaultDescription
valuenumber0Target value
durationnumber2000Animation duration in milliseconds
decimalsnumber0Number of decimal places
useGroupingbooleantrueUse thousands separator
easingstring'easeOutQuart'Easing function
formatter(value: number) => string-Custom formatter function

Easing Functions

Available easing functions:

  • linear
  • easeInQuad, easeOutQuad, easeInOutQuad
  • easeInCubic, easeOutCubic, easeInOutCubic
  • easeInQuart, easeOutQuart, easeInOutQuart
  • easeInExpo, easeOutExpo, easeInOutExpo

Examples

Currency

vue
<template>
  <span v-counter="{
    value: 1234.56,
    decimals: 2,
    formatter: (v) => '$' + v.toLocaleString('en-US', { minimumFractionDigits: 2 })
  }">0</span>
</template>

Large Numbers

vue
<template>
  <span v-counter="{
    value: 1000000,
    duration: 3000,
    formatter: (v) => {
      if (v >= 1000000) return (v / 1000000).toFixed(1) + 'M'
      if (v >= 1000) return (v / 1000).toFixed(1) + 'K'
      return v.toString()
    }
  }">0</span>
</template>

Composable API

For programmatic use, you can use the useCounter composable:

typescript
import { useCounter } from 'directix'

const { start, pause, reset, update, currentValue } = useCounter({
  startValue: 0,
  endValue: 1000,
  duration: 2000,
  formatter: (v) => `$${v.toFixed(2)}`
})

// Start animation
start()

// Pause animation
pause()

// Reset to start value
reset()

// Update end value
update(2000)

UseCounterReturn

PropertyTypeDescription
start() => voidStart animation
pause() => voidPause animation
reset() => voidReset to start value
update(value: number) => voidUpdate end value
currentValueRef<number>Current animated value

Code Generator

Quick Code Generator
<template>
  <div v-counter="{ value: 1000, duration: 2000 }">
    <!-- Display animated number counter. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = { value: 1000, duration: 2000 }
</script>

Released under the MIT License.