index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <script lang="ts" setup>
  2. import type { CountToProps, TransitionPresets } from '@vben/common-ui';
  3. import { reactive } from 'vue';
  4. import { CountTo, Page, TransitionPresetsKeys } from '@vben/common-ui';
  5. import { IconifyIcon } from '@vben/icons';
  6. import {
  7. Button,
  8. Card,
  9. Col,
  10. Form,
  11. FormItem,
  12. Input,
  13. InputNumber,
  14. message,
  15. Row,
  16. Select,
  17. Switch,
  18. } from 'ant-design-vue';
  19. const props = reactive<CountToProps & { transition: TransitionPresets }>({
  20. decimal: '.',
  21. decimals: 2,
  22. decimalStyle: {
  23. fontSize: 'small',
  24. fontStyle: 'italic',
  25. },
  26. delay: 0,
  27. disabled: false,
  28. duration: 2000,
  29. endVal: 100_000,
  30. mainStyle: {
  31. color: 'hsl(var(--primary))',
  32. fontSize: 'xx-large',
  33. fontWeight: 'bold',
  34. },
  35. prefix: '¥',
  36. prefixStyle: {
  37. paddingRight: '0.5rem',
  38. },
  39. separator: ',',
  40. startVal: 0,
  41. suffix: '元',
  42. suffixStyle: {
  43. paddingLeft: '0.5rem',
  44. },
  45. transition: 'easeOutQuart',
  46. });
  47. function changeNumber() {
  48. props.endVal =
  49. Math.floor(Math.random() * 100_000_000) / 10 ** (props.decimals || 0);
  50. }
  51. function openDocumentation() {
  52. window.open('https://vueuse.org/core/useTransition/', '_blank');
  53. }
  54. function onStarted() {
  55. message.loading({
  56. content: '动画已开始',
  57. duration: 0,
  58. key: 'animator-info',
  59. });
  60. }
  61. function onFinished() {
  62. message.success({
  63. content: '动画已结束',
  64. duration: 2,
  65. key: 'animator-info',
  66. });
  67. }
  68. </script>
  69. <template>
  70. <Page title="CountTo" description="数字滚动动画组件。使用">
  71. <template #description>
  72. <span>
  73. 使用useTransition封装的数字滚动动画组件,每次改变当前值都会产生过渡动画。
  74. </span>
  75. <Button type="link" @click="openDocumentation">
  76. 查看useTransition文档
  77. </Button>
  78. </template>
  79. <Card title="基本用法">
  80. <div class="flex w-full items-center justify-center pb-4">
  81. <CountTo v-bind="props" @started="onStarted" @finished="onFinished" />
  82. </div>
  83. <Form :model="props">
  84. <Row :gutter="20">
  85. <Col :span="8">
  86. <FormItem label="初始值" name="startVal">
  87. <InputNumber v-model:value="props.startVal" />
  88. </FormItem>
  89. </Col>
  90. <Col :span="8">
  91. <FormItem label="当前值" name="endVal">
  92. <InputNumber
  93. v-model:value="props.endVal"
  94. class="w-full"
  95. :precision="props.decimals"
  96. >
  97. <template #addonAfter>
  98. <IconifyIcon
  99. v-tippy="`设置一个随机值`"
  100. class="size-5 cursor-pointer outline-none"
  101. icon="ix:random-filled"
  102. @click="changeNumber"
  103. />
  104. </template>
  105. </InputNumber>
  106. </FormItem>
  107. </Col>
  108. <Col :span="8">
  109. <FormItem label="禁用动画" name="disabled">
  110. <Switch v-model:checked="props.disabled" />
  111. </FormItem>
  112. </Col>
  113. <Col :span="8">
  114. <FormItem label="延迟动画" name="delay">
  115. <InputNumber v-model:value="props.delay" :min="0" />
  116. </FormItem>
  117. </Col>
  118. <Col :span="8">
  119. <FormItem label="持续时间" name="duration">
  120. <InputNumber v-model:value="props.duration" :min="0" />
  121. </FormItem>
  122. </Col>
  123. <Col :span="8">
  124. <FormItem label="小数位数" name="decimals">
  125. <InputNumber
  126. v-model:value="props.decimals"
  127. :min="0"
  128. :precision="0"
  129. />
  130. </FormItem>
  131. </Col>
  132. <Col :span="8">
  133. <FormItem label="分隔符" name="separator">
  134. <Input v-model:value="props.separator" />
  135. </FormItem>
  136. </Col>
  137. <Col :span="8">
  138. <FormItem label="小数点" name="decimal">
  139. <Input v-model:value="props.decimal" />
  140. </FormItem>
  141. </Col>
  142. <Col :span="8">
  143. <FormItem label="动画" name="transition">
  144. <Select v-model:value="props.transition">
  145. <Select.Option
  146. v-for="preset in TransitionPresetsKeys"
  147. :key="preset"
  148. :value="preset"
  149. >
  150. {{ preset }}
  151. </Select.Option>
  152. </Select>
  153. </FormItem>
  154. </Col>
  155. <Col :span="8">
  156. <FormItem label="前缀" name="prefix">
  157. <Input v-model:value="props.prefix" />
  158. </FormItem>
  159. </Col>
  160. <Col :span="8">
  161. <FormItem label="后缀" name="suffix">
  162. <Input v-model:value="props.suffix" />
  163. </FormItem>
  164. </Col>
  165. </Row>
  166. </Form>
  167. </Card>
  168. </Page>
  169. </template>