微信小程序通過(guò)自定義animate-numbers組件實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果
微信小程序中實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果
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);
}
},
// 組件被移除時(shí)清除定時(shí)器
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)于微信小程序中實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)數(shù)字跳動(dòng)效果(自定義animate-numbers組件)的文章就介紹到這了,更多相關(guān)小程序數(shù)字跳動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js數(shù)組常用操作方法小結(jié)(增加,刪除,合并,分割等)
這篇文章主要介紹了js數(shù)組常用操作方法,結(jié)合實(shí)例總結(jié)了javascript數(shù)組的增加、刪除、合并、分割等操作技巧,需要的朋友可以參考下2016-08-08
JS實(shí)現(xiàn)的簡(jiǎn)單折疊展開(kāi)動(dòng)畫(huà)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單折疊展開(kāi)動(dòng)畫(huà)效果,可實(shí)現(xiàn)類(lèi)似百度頁(yè)面分享按鈕一樣的折疊展開(kāi)動(dòng)畫(huà)效果,涉及javascript頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04
通過(guò)原生JS實(shí)現(xiàn)為元素添加事件的方法
下面小編就為大家?guī)?lái)一篇通過(guò)原生JS實(shí)現(xiàn)為元素添加事件的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
JavaScript編寫(xiě)一個(gè)簡(jiǎn)易購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了JavaScript簡(jiǎn)易購(gòu)物車(chē)功能的編寫(xiě)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
javascript實(shí)現(xiàn)數(shù)字配對(duì)游戲的實(shí)例講解
下面小編就為大家分享一篇javascript實(shí)現(xiàn)數(shù)字配對(duì)游戲的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)N秒后網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)代碼,非常的實(shí)用,這里推薦給大家。2014-12-12
JavaScript原生節(jié)點(diǎn)操作小結(jié)
本文主要介紹了JavaScript原生節(jié)點(diǎn)操作的相關(guān)知識(shí)。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
three.js實(shí)現(xiàn)3D影院的原理的代碼分析
本篇文章主要給大家講解了如何通過(guò)three.js實(shí)現(xiàn)3D影院的功能以及原理分析,需要的朋友參考一下吧。2017-12-12

