微信小程序通過自定義animate-numbers組件實現(xiàn)進入頁面時數(shù)字跳動效果
微信小程序中實現(xiàn)進入頁面時數(shù)字跳動效果
1. 組件定義,新建animate-numbers組件
1.1 index.js
// components/animate-numbers/index.js
Component({
properties: {
number: {
type: Number,
value: 0
},
duration: {
type: Number,
value: 1000
}
},
data: {
displayNumber: 0,
animationFrameId: null
},
observers: {
'number': function (newNumber) {
this.animateNumber(newNumber);
}
},
methods: {
animateNumber(targetNumber) {
const start = this.data.displayNumber;//舊值
const end = targetNumber;//新值
const duration = this.properties.duration;
const increment = (end - start) / (duration / 16); // 假設(shè)每秒60幀,每幀間隔約16ms
let current = start;
if(this.data.animationFrameId){
clearInterval(this.data.animationFrameId);
}
const animate = () => {
current += increment;
if ((increment > 0 && current >= end) || (increment < 0 && current <= end)) {
clearInterval(this.data.animationFrameId);
this.setData({ displayNumber: end });
} else {
this.setData({ displayNumber: Math.round(current) });
}
};
this.data.animationFrameId = setInterval(animate, 16);
}
},
// 組件被移除時清除定時器
detached() {
clearInterval(this.data.animationFrameId);
}
});1.2 wxml
<view>{{displayNumber}}</view>1.3 wxss
page {
font-size: 48rpx;
font-weight: bold;
}2. 使用組件
"animate-numbers": "../../../components/animate-numbers/index"
<animate-numbers number="{{attendanceInfo.month_avg_days}}" duration="1000"/>到此這篇關(guān)于微信小程序中實現(xiàn)進入頁面時數(shù)字跳動效果(自定義animate-numbers組件)的文章就介紹到這了,更多相關(guān)小程序數(shù)字跳動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js數(shù)組常用操作方法小結(jié)(增加,刪除,合并,分割等)
這篇文章主要介紹了js數(shù)組常用操作方法,結(jié)合實例總結(jié)了javascript數(shù)組的增加、刪除、合并、分割等操作技巧,需要的朋友可以參考下2016-08-08
javascript實現(xiàn)數(shù)字配對游戲的實例講解
下面小編就為大家分享一篇javascript實現(xiàn)數(shù)字配對游戲的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
javascript實現(xiàn)倒計時N秒后網(wǎng)頁自動跳轉(zhuǎn)代碼
這篇文章主要介紹了javascript實現(xiàn)倒計時N秒后網(wǎng)頁自動跳轉(zhuǎn)代碼,非常的實用,這里推薦給大家。2014-12-12

