找回密码
 立即注册

扫一扫,访问微社区

查看: 4223|回复: 3

微信小程序学习用demo:数字累加,动态效果

[复制链接]
发表于 2017-1-17 12:47:49 | 显示全部楼层 |阅读模式


[mw_shl_code=html,true]<!--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>[/mw_shl_code]

[mw_shl_code=javascript,true]// 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:" 完成了"
            });
        }
    });
}
})[/mw_shl_code]


[mw_shl_code=javascript,true]/**
* Created by wangyy on 2016/12/26.
*/
'use strict';
class NumberAnimate {

    constructor(opt) {
        let def = {
            from:50,//开始时的数字
            speed:2000,// 总时间
            refreshTime:100,// 刷新一次的时间
            decimals:2,// 小数点后的位数
            onUpdate:function(){}, // 更新时回调函数
            onComplete:function(){} // 完成时回调函数
        }
        this.tempValue = 0;//累加变量值
        this.opt = Object.assign(def,opt);//assign传入配置参数
        this.loopCount = 0;//循环次数计数
        this.loops = Math.ceil(this.opt.speed/this.opt.refreshTime);//数字累加次数
        this.increment = (this.opt.from/this.loops);//每次累加的值
        this.interval = null;//计时器对象
        this.init();
    }
    init(){
        this.interval = setInterval(()=>{this.updateTimer()},this.opt.refreshTime);
    }

    updateTimer(){
         
        this.loopCount++;
        this.tempValue = this.formatFloat(this.tempValue,this.increment).toFixed(this.opt.decimals);
        if(this.loopCount >= this.loops){
            clearInterval(this.interval);
            this.tempValue = this.opt.from;
            this.opt.onComplete();
        }
        this.opt.onUpdate();
    }
    //解决0.1+0.2不等于0.3的小数累加精度问题
    formatFloat(num1, num2) {
        let baseNum, baseNum1, baseNum2;
        try {
            baseNum1 = num1.toString().split(".")[1].length;
        } catch (e) {
            baseNum1 = 0;
        }
        try {
            baseNum2 = num2.toString().split(".")[1].length;
        } catch (e) {
            baseNum2 = 0;
        }
        baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
        return (num1 * baseNum + num2 * baseNum) / baseNum;
    };
}
export default  NumberAnimate;[/mw_shl_code]


游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

0

主题

245

回帖

63

金钱

新人求带

积分
0
发表于 2017-8-18 09:18:09 | 显示全部楼层
感谢分享
回复

使用道具 举报

0

主题

2

回帖

9

金钱

新人求带

积分
0
发表于 2019-11-9 12:20:51 来自手机 | 显示全部楼层
学习一下
回复

使用道具 举报

0

主题

353

回帖

830

金钱

新人求带

积分
0
发表于 2020-4-2 15:30:05 | 显示全部楼层
我有流量,你有产品,我们就可以合作,有意的个人开发者朋友可以联系微信号jim20180688,详聊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|微信小程序开发|教程|文档|资源汇总_即速论坛 ( 粤ICP备14097199号-1  

GMT+8, 2024-4-20 10:53 , Processed in 0.139593 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表