Skip to content

CountTo

Count animation is a commonly used component, included in the component library for convenience. The source code is based on vue-count-to, with the same props. Since it only displays numbers, all styles are customizable.

Basic Usage

0
<template>
  <div>
    <b-button type="primary" @click="restart">restart</b-button>
    <div class="mt-15" flex="cross:center">
      <b-tag type="primary" dot font-size="14px">
        <b-count-to ref="countToRef" :start-val="startVal" :end-val="endVal"></b-count-to>
      </b-tag>
    </div>
  </div>
</template>

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

const startVal = ref(0)
const endVal = ref(2023)
const countToRef = ref(null)

function restart() {
  countToRef.value?.restart()
}
</script>

Customparameter

Can set different parameters to define duration, prefix, suffix, separator, etc. for the display

0
0.00
0
$0美金
<template>
  <div>
    <b-button type="primary" @click="restart">restart</b-button>
    <div class="mt-15" flex="cross:center">
      <div style="font-size: 18px; color: #1089ff; width: 120px; margin-right: 16px">
        <b-count-to
          ref="countTo1"
          :start-val="startVal"
          :end-val="endVal"
          :duration="3000"
        ></b-count-to>
      </div>
      <div style="font-size: 18px; color: #1089ff; width: 120px; margin-right: 16px">
        <b-count-to
          ref="countTo2"
          :start-val="startVal"
          :end-val="endVal"
          :duration="3000"
          :decimals="2"
        ></b-count-to>
      </div>
      <div style="font-size: 18px; color: #1089ff; width: 120px; margin-right: 16px">
        <b-count-to
          ref="countTo3"
          :start-val="startVal"
          :end-val="endVal"
          :duration="3000"
          separator=""
        ></b-count-to>
      </div>
      <div style="font-size: 18px; color: #1089ff; width: 120px; margin-right: 16px">
        <b-count-to
          ref="countTo4"
          :start-val="startVal"
          :end-val="endVal"
          :duration="3000"
          prefix="$"
          suffix="美金"
        ></b-count-to>
      </div>
    </div>
  </div>
</template>

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

const startVal = ref(0)
const endVal = ref(2023)
const countTo1 = ref(null)
const countTo2 = ref(null)
const countTo3 = ref(null)
const countTo4 = ref(null)

function restart() {
  countTo1.value?.restart()
  countTo2.value?.restart()
  countTo3.value?.restart()
  countTo4.value?.restart()
}
</script>

Props

ParameterDescriptionTypeOptionsDefault
startValStart valueNumber-
endValEnd valueNumber2000
durationAnimation durationNumber2000
autoplayWhether to auto playBooleantrue
decimalsDecimal precisionNumber0
decimalDecimal point displayString.
separatorDigit separatorString,
prefixPrefix, character shown before the valueString
suffixSuffix, character shown after the valueString

Events

Event NameDescriptionReturn Value
mountedStart render
callbackCallback when finished

Functions

Event NameDescriptionParameter
countGet current valuetimestamp, time
startStart
pauseResumePause and resume
resetReset
restartRestart