2024-01-24 01:48:23 +08:00
|
|
|
<script setup lang="ts">
|
2024-01-25 23:16:12 +08:00
|
|
|
import { computed, nextTick, ref, watch } from 'vue';
|
2024-01-24 01:48:23 +08:00
|
|
|
import { TransitionPresets, useTransition } from '@vueuse/core';
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'CountTo'
|
|
|
|
});
|
|
|
|
|
2024-01-25 23:24:35 +08:00
|
|
|
interface Props {
|
|
|
|
startValue?: number;
|
|
|
|
endValue?: number;
|
|
|
|
duration?: number;
|
|
|
|
autoplay?: boolean;
|
|
|
|
decimals?: number;
|
|
|
|
prefix?: string;
|
|
|
|
suffix?: string;
|
|
|
|
separator?: string;
|
|
|
|
decimal?: string;
|
|
|
|
useEasing?: boolean;
|
|
|
|
transition?: keyof typeof TransitionPresets;
|
|
|
|
}
|
|
|
|
|
2024-01-24 01:48:23 +08:00
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
startValue: 0,
|
2024-01-25 23:16:12 +08:00
|
|
|
endValue: 2021,
|
|
|
|
duration: 1500,
|
2024-01-24 01:48:23 +08:00
|
|
|
autoplay: true,
|
|
|
|
decimals: 0,
|
|
|
|
prefix: '',
|
|
|
|
suffix: '',
|
|
|
|
separator: ',',
|
2024-01-25 23:16:12 +08:00
|
|
|
decimal: '.',
|
|
|
|
useEasing: true,
|
|
|
|
transition: 'linear'
|
2024-01-24 01:48:23 +08:00
|
|
|
});
|
|
|
|
|
2024-01-25 23:16:12 +08:00
|
|
|
const source = ref(props.startValue);
|
|
|
|
|
|
|
|
const transition = computed(() => (props.useEasing ? TransitionPresets[props.transition] : undefined));
|
2024-01-24 01:48:23 +08:00
|
|
|
|
|
|
|
const outputValue = useTransition(source, {
|
|
|
|
disabled: false,
|
2024-01-25 23:16:12 +08:00
|
|
|
duration: props.duration,
|
|
|
|
transition: transition.value
|
2024-01-24 01:48:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const value = computed(() => formatValue(outputValue.value));
|
|
|
|
|
|
|
|
function formatValue(num: number) {
|
|
|
|
const { decimals, decimal, separator, suffix, prefix } = props;
|
|
|
|
|
|
|
|
let number = num.toFixed(decimals);
|
|
|
|
number = String(number);
|
|
|
|
|
|
|
|
const x = number.split('.');
|
|
|
|
let x1 = x[0];
|
|
|
|
const x2 = x.length > 1 ? decimal + x[1] : '';
|
|
|
|
const rgx = /(\d+)(\d{3})/;
|
|
|
|
if (separator) {
|
|
|
|
while (rgx.test(x1)) {
|
|
|
|
x1 = x1.replace(rgx, `$1${separator}$2`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + x1 + x2 + suffix;
|
|
|
|
}
|
|
|
|
|
2024-01-25 23:16:12 +08:00
|
|
|
async function start() {
|
|
|
|
await nextTick();
|
|
|
|
source.value = props.endValue;
|
|
|
|
}
|
|
|
|
|
2024-01-24 01:48:23 +08:00
|
|
|
watch(
|
|
|
|
[() => props.startValue, () => props.endValue],
|
|
|
|
() => {
|
|
|
|
if (props.autoplay) {
|
2024-01-25 23:16:12 +08:00
|
|
|
start();
|
2024-01-24 01:48:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ immediate: true }
|
|
|
|
);
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<span>{{ value }}</span>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped></style>
|