微信小程序 animation API詳解及實例代碼
動畫水還是比較深的,這里只是簡單介紹下小程序中動畫的一些屬性和注意事項,做動畫前一定要整理好思路將動畫一步步分解,再進行組合!這里只做引入。
wx.createAnimation(object)
看官方介紹
1.創(chuàng)建一個動畫實例animation。調(diào)用實例的方法來描述動畫。最后通過動畫實例的export方法導出動畫數(shù)據(jù)傳遞給組件的animation屬性。
2.調(diào)用動畫操作方法后要調(diào)用 step() 來表示一組動畫完成,可以在一組動畫中調(diào)用任意多個動畫方法,一組動畫中的所有動畫會同時開始,一組動畫完成后才會進行下一組動畫。step 可以傳入一個跟 wx.createAnimation() 一樣的配置參數(shù)用于指定當前組動畫的屬性
這還是比較好理解的比如第一條對應(yīng)代碼animation: this.animation.export()
第二條比如縮放動畫,也就說是一組scale,scaleX, scaleY…為一縮放動畫組的一個動畫方法,縮放動畫組和旋轉(zhuǎn)動畫組通過step()鏈接,按順序執(zhí)行。代碼中體驗吧!看效果反過來看會更容易理解
主要屬性:

這里主要樹下timingFunction和transformOrigin
timingFunction 設(shè)置動畫效果
- linear 默認為linear 動畫一直較為均勻
- ease 開始時緩慢中間加速到快結(jié)束時減速
- ease-in 開始的時候緩慢
- ease-in-out 開始和結(jié)束時減速
- ease-out 結(jié)束時減速
- step-start 動畫一開始就跳到 100% 直到動畫持續(xù)時間結(jié)束 一閃而過
- step-end 保持 0% 的樣式直到動畫持續(xù)時間結(jié)束 一閃而過
transformOrigin 設(shè)置動畫的基點 默認%50 %50 0
left,center right是水平方向取值,對應(yīng)的百分值為left=0%;center=50%;right=100%
top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%
動畫組及動畫方法
樣式:

旋轉(zhuǎn):

縮放:

偏移:

傾斜:

矩陣變形:

演示單個動畫組效果

wxml
<view class="container">
<view animation="{{animation}}" class="view">我在做動畫</view>
</view>
<button type="primary" bindtap="rotate">旋轉(zhuǎn)</button>
js
Page({
data:{
text:"Page animation",
animation: ''
},
onLoad:function(options){
// 頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(shù)
},
onReady:function(){
// 頁面渲染完成
//實例化一個動畫
this.animation = wx.createAnimation({
// 動畫持續(xù)時間,單位ms,默認值 400
duration: 1000,
/**
* http://cubic-bezier.com/#0,0,.58,1
* linear 動畫一直較為均勻
* ease 從勻速到加速在到勻速
* ease-in 緩慢到勻速
* ease-in-out 從緩慢到勻速再到緩慢
*
* http://www.tuicool.com/articles/neqMVr
* step-start 動畫一開始就跳到 100% 直到動畫持續(xù)時間結(jié)束 一閃而過
* step-end 保持 0% 的樣式直到動畫持續(xù)時間結(jié)束 一閃而過
*/
timingFunction: 'linear',
// 延遲多長時間開始
delay: 100,
/**
* 以什么為基點做動畫 效果自己演示
* left,center right是水平方向取值,對應(yīng)的百分值為left=0%;center=50%;right=100%
* top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%
*/
transformOrigin: 'left top 0',
success: function(res) {
console.log(res)
}
})
},
/**
* 旋轉(zhuǎn)
*/
rotate: function() {
//順時針旋轉(zhuǎn)10度
//
this.animation.rotate(150).step()
this.setData({
//輸出動畫
animation: this.animation.export()
})
},
onShow:function(){
// 頁面顯示
},
onHide:function(){
// 頁面隱藏
},
onUnload:function(){
// 頁面關(guān)閉
}
})
演示多個動畫組效果

這里我們只需要更改以下代碼即可
/**
* 旋轉(zhuǎn)
*/
rotate: function() {
//兩個動畫組 一定要以step()結(jié)尾
/**
* 動畫順序 順時針旋轉(zhuǎn)150度>x,y 放大二倍>x,y平移10px>x,y順時針傾斜>改變樣式和設(shè)置寬度寬度
*/
this.animation.rotate(150).step().scale(2).step().translate(10).step().skew(10).step().opacity(0.5).width(10).step({ducation: 8000})
this.setData({
//輸出動畫
animation: this.animation.export()
})
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
JavaScript?執(zhí)行上下文的視角詳解this使用
這篇文章主要為以JavaScript?執(zhí)行上下文的視角為大家講清楚?this使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

