微信小程序開(kāi)發(fā)代碼講解:animation循環(huán)動(dòng)畫
2017年7月13日,微信小程序開(kāi)發(fā)實(shí)例講解之微信小程序代碼講解,很多人都想問(wèn)微信小程序代碼怎么弄?下面從示例來(lái)談?wù)勎⑿判〕绦虼a中的animation循環(huán)動(dòng)畫。
7月16日開(kāi)始,阿里巴巴國(guó)際站將實(shí)施重復(fù)鋪貨處罰新規(guī)。對(duì)于重復(fù)鋪貨商品占“審核通過(guò)且已上架”商品量≥10%且重復(fù)鋪貨商品數(shù)≥100的商家,或者,店鋪重復(fù)鋪貨產(chǎn)品數(shù)≥1000的商家,阿里巴巴國(guó)際站將根據(jù)規(guī)則進(jìn)行處罰和通知。
微信小程序提供了實(shí)現(xiàn)動(dòng)畫的api——animation,但卻不能循環(huán)播放,都是一次性的,動(dòng)完就Over了,下面提供一個(gè)用微信小程序的animation來(lái)實(shí)現(xiàn)循環(huán)動(dòng)畫的玩具,拋磚引玉,希望大家能想出更好的方法來(lái)實(shí)現(xiàn)真正的循環(huán)。說(shuō)是玩具是因?yàn)檫@個(gè)循環(huán)動(dòng)畫通過(guò)js腳本的setInterval來(lái)實(shí)現(xiàn)的,但’setInterval’在實(shí)際運(yùn)行中會(huì)出現(xiàn)越來(lái)越嚴(yán)重的延遲,這是由于js的單線程運(yùn)行模式所決定的(具體可以搜本關(guān)資料看),所以動(dòng)畫間隙并不是那么流暢,所以先玩玩吧,讓我們來(lái)實(shí)現(xiàn)讓云朵飄……
讓云朵飄
實(shí)現(xiàn)代碼:
index.wxml
<view class="clouds">
<image animation="{{animationCloudData}}" class="yun1" src="../../img/yun1.png"></image>
</view>
index.js
onReady: function () {
// 頁(yè)面渲染完成
// 實(shí)例化一個(gè)動(dòng)畫
var that = this;
var i = 0
var ii = 0
var animationData = wx.createAnimation({
duration: 1000, // 默認(rèn)為400 動(dòng)畫持續(xù)時(shí)間,單位ms
timingFunction: 'ease-in-out',
//transformOrigin: '4px 91px'
});
var animationCloudData = wx.createAnimation({
duration: 1000, // 默認(rèn)為400 動(dòng)畫持續(xù)時(shí)間,單位ms
timingFunction: 'ease-in-out',
//transformOrigin: '4px 91px'
});
// 順序執(zhí)行,當(dāng)已經(jīng)執(zhí)行完上面的代碼就會(huì)開(kāi)啟定時(shí)器
// 循環(huán)執(zhí)行代碼
//dotAnFun = setInterval(function () {});
/*setInterval(function () {
// 動(dòng)畫腳本定義
//animationData.rotate(6 * (++i)).step()
//animationData.scale(2, 2).rotate(45).step().scale(1, 1).step();
animationData.translateY(10).step({ duration: 500 }).translateY(-10).step({ duration: 500 });
// 更新數(shù)據(jù)
that.setData({
// 導(dǎo)出動(dòng)畫示例
animationData: animationData.export(),
//animationCloudData: animationCloudData.export(),
})
++i;
console.log(i);
}.bind(that), 2000);//循環(huán)時(shí)間 這里1000是1秒
*/
//動(dòng)畫的腳本定義必須每次都重新生成,不能放在循環(huán)外
animationCloudData.translateX(200).step({ duration: 5000 }).translateX(0).step({ duration: 5000 });
// 更新數(shù)據(jù)
that.setData({
// 導(dǎo)出動(dòng)畫示例
//animationData: animationData.export(),
animationCloudData: animationCloudData.export(),
})
setInterval(function () {
//動(dòng)畫的腳本定義必須每次都重新生成,不能放在循環(huán)外
animationCloudData.translateX(300).step({ duration: 5000 }).translateX(-100).step({ duration: 5000 });
// 更新數(shù)據(jù)
that.setData({
// 導(dǎo)出動(dòng)畫示例
//animationData: animationData.export(),
animationCloudData: animationCloudData.export(),
})
++ii;
console.log(ii);
}.bind(that),10000);//3000這里的設(shè)置如果小于動(dòng)畫step的持續(xù)時(shí)間的話會(huì)導(dǎo)致執(zhí)行一半后出錯(cuò)
}
index.wxss
.clouds{
margin-top:320rpx;
}
.yun1{
width:320rpx;
height: 120rpx;
}
附:參考備用:
/*
var that = this;
// 頁(yè)面渲染完成
//實(shí)例化一個(gè)動(dòng)畫
var animation = wx.createAnimation({
duration: 1000,
timingFunction: 'ease',
})
this.animation = animation
animation.scale(2, 2).rotate(45).step().scale(1,1).step();
//導(dǎo)出動(dòng)畫
this.setData({
animationData: animation.export()
})
var i = 0;
// 順序執(zhí)行,當(dāng)已經(jīng)執(zhí)行完上面的代碼就會(huì)開(kāi)啟定時(shí)器
/*setTimeout(function () {
that.setData({
animationData: animation.export()
});
i++;
console.log(i);
}, 1000);*/
/*setInterval(function () {
//循環(huán)執(zhí)行代碼
that.setData({
animationData: animation.export()
});
i++;
console.log(i);
}, 1000) //循環(huán)時(shí)間 這里是1秒
}*/
想了解更多關(guān)于微信小程序開(kāi)發(fā)實(shí)例教程內(nèi)容,可以點(diǎn)擊閱讀微信小程序開(kāi)發(fā)教程。
第二部分:如何開(kāi)通一個(gè)小商店