微信小程序數(shù)字滾動(dòng)插件使用詳解
用es6語(yǔ)法方式寫(xiě)了個(gè)微信小程序小插件–數(shù)字滾動(dòng);
效果圖:

wxml頁(yè)面布局代碼:
<!--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>index.js調(diào)用NumberAnimate.js
// pages/main/index.jsimport NumberAnimate from "../../utils/NumberAnimate";Page({
data:{
},
onLoad:function(options){
// 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù)
},
onReady:function(){
},
onShow:function(){
// 頁(yè)面顯示
},
onHide:function(){
// 頁(yè)面隱藏
},
onUnload:function(){
// 頁(yè)面關(guān)閉
},
//調(diào)用NumberAnimate.js中NumberAnimate實(shí)例化對(duì)象,測(cè)試3種效果
animate:function(){
this.setData({
num1:'',
num2:'',
num3:'',
num1Complete:'',
num2Complete:'',
num3Complete:''
});
let num1 = 18362.856;
let n1 = new NumberAnimate({
from:num1,//開(kāi)始時(shí)的數(shù)字
speed:2000,// 總時(shí)間
refreshTime:100,// 刷新一次的時(shí)間
decimals:3,//小數(shù)點(diǎn)后的位數(shù)
onUpdate:()=>{//更新回調(diào)函數(shù)
this.setData({
num1:n1.tempValue });
},
onComplete:()=>{//完成回調(diào)函數(shù)
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:" 完成了"
});
}
});
}})NumberAnimate.js代碼:
/**
* Created by wangyy on 2016/12/26.
*/'use strict';class NumberAnimate {
constructor(opt) {
let def = {
from:50,//開(kāi)始時(shí)的數(shù)字
speed:2000,// 總時(shí)間
refreshTime:100,// 刷新一次的時(shí)間
decimals:2,// 小數(shù)點(diǎn)后的位數(shù)
onUpdate:function(){}, // 更新時(shí)回調(diào)函數(shù)
onComplete:function(){} // 完成時(shí)回調(diào)函數(shù)
}
this.tempValue = 0;//累加變量值
this.opt = Object.assign(def,opt);//assign傳入配置參數(shù)
this.loopCount = 0;//循環(huán)次數(shù)計(jì)數(shù)
this.loops = Math.ceil(this.opt.speed/this.opt.refreshTime);//數(shù)字累加次數(shù)
this.increment = (this.opt.from/this.loops);//每次累加的值
this.interval = null;//計(jì)時(shí)器對(duì)象
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的小數(shù)累加精度問(wèn)題
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;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)手寫(xiě)原生任務(wù)定時(shí)器
定時(shí)器顧名思義就是在某個(gè)特定的時(shí)間去執(zhí)行一些任務(wù),現(xiàn)代的應(yīng)用程序早已不是以前的那些由簡(jiǎn)單的增刪改查拼湊而成的程序了,高復(fù)雜性早已是標(biāo)配,而任務(wù)的定時(shí)調(diào)度與執(zhí)行也是對(duì)程序的基本要求了。本文將利用JavaScript手寫(xiě)原生任務(wù)定時(shí)器,需要的可以參考一下2022-03-03
JavaScript實(shí)現(xiàn)消消樂(lè)的源代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)消消樂(lè)-源代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
用javascript修復(fù)瀏覽器中頭痛問(wèn)題的方法整理篇[譯]
我們提倡無(wú)論何時(shí)都盡可能地使用CSS,這樣我們更容易取得成功.現(xiàn)在瀏覽器對(duì)CSS的支持已經(jīng)非常好,肯定足以讓你用來(lái)控制你的網(wǎng)頁(yè)布局與排版.但,即使如此,還是有某些頁(yè)面元素會(huì)在不同的瀏覽器下表現(xiàn)也不一樣.2008-11-11
一起來(lái)學(xué)習(xí)TypeScript的類(lèi)型
這篇文章主要為大家詳細(xì)介紹了TypeScript的類(lèi)型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
JavaScript調(diào)用后臺(tái)的三種方法實(shí)例
這篇文章介紹了JavaScript調(diào)用后臺(tái)的三種方法實(shí)例,有需要的朋友可以參考一下2013-10-10
微信小程序仿微信運(yùn)動(dòng)步數(shù)排行(交互)
這篇文章主要介紹了微信小程序仿微信運(yùn)動(dòng)步數(shù)排行(交互),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
js 第二代身份證號(hào)碼的驗(yàn)證機(jī)制代碼
在盛大某網(wǎng)站注冊(cè)的時(shí)候,身份證必填,但我又不想填真實(shí)身份證號(hào)碼,于是隨便編了串自認(rèn)為合法的身份證號(hào)碼,但是卻馬上被提示號(hào)碼錯(cuò)誤2011-05-05
disable-devtool禁用web開(kāi)發(fā)者工具保護(hù)網(wǎng)頁(yè)源碼
這篇文章主要為大家介紹了disable-devtool禁用web開(kāi)發(fā)者工具保護(hù)網(wǎng)頁(yè)源碼的使用,防止源碼泄露保護(hù)網(wǎng)站源碼的最佳解決方案,一行代碼就可以搞定,有需要的可以學(xué)習(xí)參考下2023-11-11

