微信小程序API 動畫

2018-08-16 15:48 更新

wx.createAnimation(OBJECT)


? 創(chuàng)建一個動畫實例animation。調(diào)用實例的方法來描述動畫。最后通過動畫實例的export方法導(dǎo)出動畫數(shù)據(jù)傳遞給組件的animation屬性。

注意:export方法每次調(diào)用后會清掉之前的動畫操作

OBJECT參數(shù)說明:

參數(shù)類型必填默認值說明
durationInteger400動畫持續(xù)時間,單位ms
timingFunctionString"linear"定義動畫的效果
delayInteger0動畫延遲時間,單位 ms
transformOriginString"50% 50% 0"設(shè)置transform-origin

timingFunction 有效值:

說明
linear動畫從頭到尾的速度是相同的
ease動畫以低速開始,然后加快,在結(jié)束前變慢
ease-in動畫以低速開始
ease-in-out動畫以低速開始和結(jié)束
ease-out動畫以低速結(jié)束
step-start動畫第一幀就跳至結(jié)束狀態(tài)直到結(jié)束
step-end動畫一直保持開始狀態(tài),最后一幀跳到結(jié)束狀態(tài)

var animation = wx.createAnimation({
  transformOrigin:"50% 50%",
  duration:1000,
  timingFunction:"ease",
  delay:0
})

animation


動畫實例可以調(diào)用以下方法來描述動畫,調(diào)用結(jié)束后會返回自身,支持鏈式調(diào)用的寫法。

animation 對象的方法列表:

樣式:

方法 參數(shù) 說明
opacity value 透明度,參數(shù)范圍 0~1
backgroundColor color 顏色值
width length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值
height length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值
top length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值
left length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值
bottom length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值
right length 長度值,如果傳入 Number 則默認使用 px,可傳入其他自定義單位的長度值

旋轉(zhuǎn):

方法 參數(shù) 說明
rotate deg deg的范圍-180~180,從原點順時針旋轉(zhuǎn)一個deg角度
rotateX deg deg的范圍-180~180,在X軸旋轉(zhuǎn)一個deg角度
rotateY deg deg的范圍-180~180,在Y軸旋轉(zhuǎn)一個deg角度
rotateZ deg deg的范圍-180~180,在Z軸旋轉(zhuǎn)一個deg角度
rotate3d (x,y,z,deg) transform-function rotate3d

縮放:

方法 參數(shù) 說明
scale sx,[sy] 一個參數(shù)時,表示在X軸、Y軸同時縮放sx倍數(shù);兩個參數(shù)時表示在X軸縮放sx倍數(shù),在Y軸縮放sy倍數(shù)
scaleX sx 在X軸縮放sx倍數(shù)
scaleY sy 在Y軸縮放sy倍數(shù)
scaleZ sz 在Z軸縮放sy倍數(shù)
scale3d (sx,sy,sz) 在X軸縮放sx倍數(shù),在Y軸縮放sy倍數(shù),在Z軸縮放sz倍數(shù)

偏移:

方法 參數(shù) 說明
translate tx,[ty] 一個參數(shù)時,表示在X軸偏移tx,單位px;兩個參數(shù)時,表示在X軸偏移tx,在Y軸偏移ty,單位px。
translateX tx 在X軸偏移tx,單位px
translateY ty 在Y軸偏移tx,單位px
translateZ tz 在Z軸偏移tx,單位px
translate3d (tx,ty,tz) 在X軸偏移tx,在Y軸偏移ty,在Z軸偏移tz,單位px

傾斜:

方法 參數(shù) 說明
skew ax,[ay] 參數(shù)范圍-180~180;一個參數(shù)時,Y軸坐標不變,X軸坐標延順時針傾斜ax度;兩個參數(shù)時,分別在X軸傾斜ax度,在Y軸傾斜ay度
skewX ax 參數(shù)范圍-180~180;Y軸坐標不變,X軸坐標延順時針傾斜ax度
skewY ay 參數(shù)范圍-180~180;X軸坐標不變,Y軸坐標延順時針傾斜ay度

矩陣變形:

方法 參數(shù) 說明
matrix (a,b,c,d,tx,ty) transform-function matrix
matrix3d   transform-function matrix3d

動畫隊列


調(diào)用動畫操作方法后要調(diào)用step()來表示一組動畫完成,可以在一組動畫中調(diào)用任意多個動畫方法,一組動畫中的所有動畫會同時開始,一組動畫完成后才會進行下一組動畫。step 可以傳入一個跟wx.createAnimation()一樣的配置參數(shù)用于指定當前組動畫的配置。

示例:

<view animation="{{animationData}}" style="background:red,height:100rpx,width:100rpx"></view>
Page({
  data:{
    animationData:{}
  },
  onShow:function(){
    var animation = wx.createAnimation({
      duration:1000,
        timingFunction:"ease",
    })

    this.animation = animation

    animation.scale(2,2).rotate(45).step();

    this.setData({
      animationData:animation.export()
    })

    setTimeout(function(){
      animation.translate(30).step();
      this.setData({
        animationData:animation.export()
      })
    }.bind(this),1000)
  },
  rotateAndScale: function () {
    // 旋轉(zhuǎn)同時放大
    this.animation.rotate(45).scale(2, 2).step()
    this.setData({
      animationData:animation.export()
    })
  },
  rotateThenScale: function () {
    // 先旋轉(zhuǎn)后放大
    this.animation.rotate(45).step()
    this.animation.scale(2, 2).step()
    this.setData({
      animationData:animation.export()
    })
  },
  rotateAndScaleThenTranslate: function () {
    // 先旋轉(zhuǎn)同時放大,然后平移
    this.animation.rotate(45).scale(2, 2).step()
    this.animation.translate(100, 100).step({ duration: 1000 })
    this.setData({
      animationData:animation.export()
    })
  }
})

bug & tip

  1. bugiOS/Android6.3.30通過 step() 分隔動畫,只有第一步動畫能生效

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號