小程序开发|小程序制作|小程序开发网

搜索

微信小程序数字滚动小插件开发教程

2022-6-25 17:12| 发布者: 孙老大| 查看: 242| 评论: 5

摘要: 用es6语法方式写了个微信小程序小插件–数字滚动;效果图:    wxml页面布局代码:{{num1}}{{num1Complete}}{{num2}}{{num2Complete}}{{num3}}{{num3Complete}}click meindex.js调用NumberAnimate.js// pa


es6语法方式写了个微信小程序小插件数字滚动;效果图:

  
  1. wxml页面布局代码:

  2. {{num1}}{{num1Complete}}{{num2}}{{num2Complete}}{{num3}}{{num3Complete}}click me

  3. index.js调用NumberAnimate.js

  4. // pages/main/index.jsimport NumberAnimate from "../../utils/NumberAnimate";Page({
  5.   data:{
  6.    
  7.   },
  8.   onLoad:function(options){
  9.     // 页面初始化 options为页面跳转所带来的参数
  10.    
  11.   },
  12.   onReady:function(){
  13.    
  14.   },
  15.   onShow:function(){
  16.    
  17.     // 页面显示
  18.   },
  19.   onHide:function(){
  20.     // 页面隐藏
  21.   },

  22.   onUnload:function(){
  23.     // 页面关闭

  24.   },
  25.   //调用NumberAnimate.js中NumberAnimate实例化对象,测试3种效果
  26. animate:function(){

  27.    this.setData({
  28.      num1:'',
  29.      num2:'',
  30.      num3:'',
  31.      num1Complete:'',
  32.      num2Complete:'',
  33.      num3Complete:''
  34.    });

  35.     let num1 = 18362.856;
  36.     let n1 = new NumberAnimate({
  37.         from:num1,//开始时的数字
  38.         speed:2000,// 总时间
  39.         refreshTime:100,//  刷新一次的时间
  40.         decimals:3,//小数点后的位数
  41.         onUpdate:()=>{//更新回调函数
  42.           this.setData({
  43.             num1:n1.tempValue          });
  44.         },
  45.         onComplete:()=>{//完成回调函数
  46.             this.setData({
  47.               num1Complete:" 完成了"
  48.             });
  49.         }
  50.     });

  51.     let num2 = 13388;
  52.     let n2 = new NumberAnimate({
  53.         from:num2,
  54.         speed:1500,
  55.         decimals:0,
  56.         refreshTime:100,
  57.         onUpdate:()=>{
  58.           this.setData({
  59.             num2:n2.tempValue          });
  60.         },
  61.         onComplete:()=>{
  62.             this.setData({
  63.               num2Complete:" 完成了"
  64.             });
  65.         }
  66.     });

  67.     let num3 = 2123655255888.86;
  68.     let n3 = new NumberAnimate({
  69.         from:num3,
  70.         speed:2000,
  71.         refreshTime:100,
  72.         decimals:2,
  73.         onUpdate:()=>{
  74.           this.setData({
  75.             num3:n3.tempValue          });
  76.         },
  77.         onComplete:()=>{
  78.             this.setData({
  79.               num3Complete:" 完成了"
  80.             });
  81.         }
  82.     });
  83. }})

  84. NumberAnimate.js代码:

  85. /**
  86. * Created by wangyy on 2016/12/26.
  87. */'use strict';class NumberAnimate {

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

  108.     updateTimer(){
  109.         
  110.         this.loopCount++;
  111.         this.tempValue = this.formatFloat(this.tempValue,this.increment).toFixed(this.opt.decimals);
  112.         if(this.loopCount >= this.loops){
  113.             clearInterval(this.interval);
  114.             this.tempValue = this.opt.from;
  115.             this.opt.onComplete();
  116.         }
  117.         this.opt.onUpdate();
  118.     }
  119.     //解决0.1+0.2不等于0.3的小数累加精度问题
  120.     formatFloat(num1, num2) {
  121.         let baseNum, baseNum1, baseNum2;
  122.         try {
  123.             baseNum1 = num1.toString().split(".")[1].length;
  124.         } catch (e) {
  125.             baseNum1 = 0;
  126.         }
  127.         try {
  128.             baseNum2 = num2.toString().split(".")[1].length;
  129.         } catch (e) {
  130.             baseNum2 = 0;
  131.         }
  132.         baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  133.         return (num1 * baseNum + num2 * baseNum) / baseNum;
  134.     };}export default  NumberAnimate;




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

鲜花

握手

雷人

路过

鸡蛋
发表评论

最新评论

引用 心影大都 2022-6-25 18:24
laikankankank
引用 孙老大 2022-6-25 18:07
这个代码实用
引用 孙老大 2022-6-25 17:52
这个代码实用
引用 孙老大 2022-6-25 17:40
这个代码实用
引用 蠡湖冲 2022-6-25 17:33
好 学习学习

查看全部评论(5)

返回顶部