1.0.0 • Published 5 years ago

wx-countup v1.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

WxCountUp - 数字滚动

插件介绍

该插件是参照 CountUp.js 封装的,为小程序提供的数字滚动,以更有趣的方式显示数值数据。

WxCountUp

参数说明

Params

参数类型描述
targetString滚动对象
endValNumber滚动结束时的数字
optionsObject配置
contextObject微信小程序当前this对象,必填

Options (非必填项)

参数类型描述
startValNumber滚动开始时的数字,默认为0
decimalPlacesNumber小数位数,默认为0
durationNumber动画间隔时间,默认为2
useGroupingBoolean是否按组间隔,默认为true。例如:1,000 vs 1000
useEasingBoolean是否使用缓动效果,默认为true
smartEasingThresholdNumber如果使用缓动,则对大于此值的大数值平滑缓动,默认为999
smartEasingAmountNumber超过阈值的数字将被放宽,默认为333
separatorString按组间隔标识,默认为','
decimalString小数点标识,默认为'.'
easingFnFunction例如 (t: number, b: number, c: number, d: number) => number;
formattingFnFunction例如 (n: number) => string;
prefixString以结果为前缀的文本,默认为空
suffixString以结果为后缀的文本,默认为空
numeralsString数字符号替换

使用方法

import WxCountUp from '../../plugins/wx-countup/WxCountUp.js'

Page({
  data: {
    number: 0
  },
  onLoad: function () {
    // 最后一个参数必填
    this.countUp = new WxCountUp('number', 5234, {}, this);
  },

  start() {   
    this.countUp = new WxCountUp('number', 5234, {}, this); 
    // 开始动画
    this.countUp.start();
    // this.countUp.start(() => console.log('Complete!'));
  },

  pauseResume() {
    // 暂停/重新开始
    this.countUp.pauseResume();
  },

  reset() {
    // 重置
    this.countUp.reset();
  },

  update() {
    // 更新为新值
    this.countUp.update(1000);
  },
})

更多方法及配置参考 CountUp.js

更改部分

  • CountUp.js 源代码使用 document 浏览器提供的DOM操作接口,在微信小程序中并不支持。只能通过传入一个 this (当前上下文对象) 来 setData 改变数据。
  • 在真机调试的时候,发现真机不支持 requestAnimationFrame 方法,只得通过 setTimeout 来模拟出 requestAnimationFrame 的效果。

参考