Skip to content

CountTo 数字滚动组件

该组件一般用于需要滚动数字到某一个值的场景,目标要求是一个递增的值。

温馨提示

本项目参考了 uView-Plus 开源项目的组件开发方式,基于 Vue 3 和 TypeScript 实现了自定义组件。目前该组件仍处于测试阶段。
感谢 uView-Plus 开源项目及其团队成员的贡献,他们的组件开发思路为本项目提供了宝贵地参考。如果需要了解更多组件开发细节,可以参考uView-Plus的 countTo组件 的代码实现。

平台差异说明

APP(vue)H5微信小程序

基本使用示例

html
<!-- 全局使用 -->
<hy-count-to time="3600 * 30 * 1000"></hy-count-to>
<!-- 单个组件引入 -->
<HyCountTo  time="3600 * 30 * 1000"></HyButton>
ts
import { HyCountTo } from "hy-app"

显示小数点

  • 通过设置decimals来设置几位小数点
html
<template>
    <hy-count-to :endVal="1542221" :decimals="2"></hy-count-to>
</template>

千分位分隔符

  • 通过设置separator来设置千分位分隔符
html
<template>
    <hy-count-to :endVal="1542221" separator=","></hy-count-to>
</template>

设置滚动时间

  • 通过设置duration来设置滚动时长
html
<template>
    <hy-count-to :endVal="1542221" :duration="10000"></hy-count-to>
</template>

设置字体

  • 通过设置bold把字体变粗
  • 通过设置fontSize设置字体大小
  • 通过设置color设置字体颜色
html
<template>
    <hy-count-to :endVal="154" bold fontSize="40" color="#31E749"></hy-count-to>
</template>

手动控制

html
<hy-count-to
        ref="countToRef"
        :endVal="1542222"
        :duration="10000"
        :autoplay="false"
></hy-count-to>
<view class="hy-flex">
    <hy-button text="开始" type="success" @click="start"></hy-button>
    <hy-button text="暂停" type="error" @click="pause"></hy-button>
    <hy-button text="继续" type="info" @click="resume"></hy-button>
</view>
ts
import HyCountTo from "hy-app/components/hy-count-to/hy-count-to.vue";
import { ref } from "vue";


const countToRef = ref<InstanceType<typeof HyCountTo>>();

const start = () => {
  if (countToRef.value) {
      countToRef.value.start();
  }
};

const pause = () => {
  if (countToRef.value) {
      countToRef.value.pause();
  }
};

const resume = () => {
  if (countToRef.value) {
      countToRef.value.resume();
  }
};
scss
.hy-flex {
  display: flex;
  justify-content: space-around;
}

API

参数说明类型默认值
startVal开始值number0
endVal结束值number0
duration滚动过程所需的时间,单位msnumber2000
autoplay是否自动开始滚动booleantrue
decimals要显示的小数位数,见上方说明number0
useEasing滚动结束时,是否缓动结尾,见上方说明booleantrue
decimal十进制分割string,
color字体颜色string#606266
fontSize字体大小,单位pxstring|number22
bold字体是否加粗booleanfalse
separator千位分隔符,见上方说明string-

Events

事件名说明回调参数
end数值滚动到目标值时触发-

Methods

事件名说明
start开始滚动
pause暂停滚动
resume从暂停时的值开始滚动
reStart暂定状态,重新再开始滚动;或者滚动状态下,暂停

[^1] DD-日,HH-时,mm-分,ss-秒,SSS-毫秒

00:46