Created
January 21, 2026 13:47
-
-
Save Gkiokan/53e47e00690fc949a957faaf15ed80ae to your computer and use it in GitHub Desktop.
AnimateNumberV2.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <span> | |
| {{ displayNumber ? formatNumberWithDots(Math.round(displayNumber)) : '-//-' }} | |
| </span> | |
| </template> | |
| <script setup> | |
| /** | |
| Author: Gkiokan Sali | |
| Date: 2026-01-21 | |
| **/ | |
| import { ref, watch, onBeforeUnmount } from 'vue' | |
| import { formatNumberWithDots } from '~/lib/utils' | |
| const props = defineProps({ | |
| value: { | |
| type: [String,Number], | |
| default: 0, | |
| }, | |
| speed: { | |
| type: Number, | |
| default: 5 | |
| }, | |
| }); | |
| const displayNumber = ref(0); | |
| let interval = null; | |
| const stop = () => { | |
| if (interval) { | |
| clearInterval(interval) | |
| interval = null | |
| } | |
| } | |
| watch( | |
| () => props.value, | |
| (newVal) => { | |
| displayNumber.value = 0 | |
| stop() | |
| let patchedVal = Math.round(parseFloat(newVal)) | |
| if( !patchedVal ){ | |
| displayNumber.value = null | |
| stop() | |
| return; | |
| } | |
| const diff = patchedVal - displayNumber.value | |
| const steps = Math.min(Math.abs(diff), 100) | |
| const step = diff / steps | |
| let stepCount = 0 | |
| interval = setInterval(() => { | |
| stepCount++ | |
| displayNumber.value += step | |
| if (stepCount >= steps) { | |
| displayNumber.value = patchedVal | |
| stop() | |
| } | |
| }, 100 / props.speed) | |
| }, | |
| { immediate: true } | |
| ) | |
| onBeforeUnmount(() => { | |
| stop() | |
| }) | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment