這幾天在看代碼時遇到了一些問題:關於微信小程序的animation自定義動畫自己沒有系統的學習過
做動畫需要我們將一個復雜的動作過程,拆解為一步一步的小節過程
微信中已經為我們寫好了端口我們只需要實例化一個動畫實例(實例代碼如下)
先了解基礎部分:
在看代碼之前要先有個下面的基礎了解
1)wx.createAnimation(object) 微信小程序實例化一個動畫效果
2)export( ) 這個方法是導出動畫數據(傳遞給animation屬性)
3)step( ) 來表示一組動畫完成
微信官網主要屬性設置:

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

旋轉:

縮放:

偏移:

傾斜:

矩形變形:

官方是這樣介紹的:
1.創建一個動畫實例animation。調用實例的方法來描述動畫。最后通過動畫實例的export方法導出動畫數據傳遞給組件的animation屬性。
這一步是基礎:
1)創建一個animation實例
2) 調用實例化的方法描述動畫
3)最后通過動畫實例的export( )方法導出動畫數據傳遞給{{animation}}
2.調用動畫操作方法后要調用 step( ) 來表示一組動畫完成,可以在一組動畫中調用任意多個動畫方法,一組動畫中的所有動畫會同時開始,一組動畫完成后才會進行下一組動畫。step 可以傳入一個跟 wx.createAnimation() 一樣的配置參數用於指定當前組動畫的屬性
下面是代碼實例:
HTML
<view class="container"> <view animation="{{animation}}" class="view">我在做動畫</view> </view> <button type="primary" bindtap="rotate">旋轉</button>
JS
Page({ data:{ text:"Page animation", animation: '' }, onLoad:function(options){ // 頁面初始化 options為頁面跳轉所帶來的參數 }, onReady:function(){ // 頁面渲染完成 //實例化一個動畫 this.animation = wx.createAnimation({ // 動畫持續時間,單位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% 直到動畫持續時間結束 一閃而過 * step-end 保持 0% 的樣式直到動畫持續時間結束 一閃而過 */ timingFunction: 'linear', // 延遲多長時間開始 delay: 100, /** * 以什么為基點做動畫 效果自己演示 * left,center right是水平方向取值,對應的百分值為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) } }) }, /** * 旋轉 */ rotate: function() { //順時針旋轉10度 // this.animation.rotate(150).step() this.setData({ //輸出動畫 animation: this.animation.export() }) } })
下面是多個動畫效果連續執行的效果(有文字描述動畫效果)
/** * 旋轉 */ rotate: function() { //兩個動畫組 一定要以step()結尾 /** * 動畫順序 順時針旋轉150度>x,y 放大二倍>x,y平移10px>x,y順時針傾斜>改變樣式和設置寬度寬度 */ 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() }) }
