<!--pages/main/index.wxml--> <view class="animate-number"> <view class="num num1">{{num1}}{{num1Complete}}</view> <view class="num num2">{{num2}}{{num2Complete}}</view> <view class="num num3">{{num3}}{{num3Complete}}</view> <view class="btn-box"> <button bindtap="animate" type="primary" class="button">click me</button> </view> </view> 2、index.js代码:
// pages/main/index.js import NumberAnimate from "../../utils/NumberAnimate"; Page({ data:{ }, onLoad:function(options){ // 页面初始化 options为页面跳转所带来的参数 }, onReady:function(){ }, onShow:function(){ // 页面显示 }, onHide:function(){ // 页面隐藏 }, onUnload:function(){ // 页面关闭 }, //调用NumberAnimate.js中NumberAnimate实例化对象,测试3种效果 animate:function(){ this.setData({ num1:'', num2:'', num3:'', num1Complete:'', num2Complete:'', num3Complete:'' }); let num1 = 18362.856; let n1 = new NumberAnimate({ from:num1,//开始时的数字 speed:2000,// 总时间 refreshTime:100,// 刷新一次的时间 decimals:3,//小数点后的位数 onUpdate:()=>{//更新回调函数 this.setData({ num1:n1.tempValue }); }, onComplete:()=>{//完成回调函数 this.setData({ num1Complete:" 完成了" }); } }); let num2 = 13388; let n2 = new NumberAnimate({ from:num2, speed:1500, decimals:0, refreshTime:100, onUpdate:()=>{ this.setData({ num2:n2.tempValue }); }, onComplete:()=>{ this.setData({ num2Complete:" 完成了" }); } }); let num3 = 2123655255888.86; let n3 = new NumberAnimate({ from:num3, speed:2000, refreshTime:100, decimals:2, onUpdate:()=>{ this.setData({ num3:n3.tempValue }); }, onComplete:()=>{ this.setData({ num3Complete:" 完成了" }); } }); } })