W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
my.createAnimation 是用于創(chuàng)建動畫實例 animation 的 API。調(diào)用實例的方法來描述動畫,最后通過動畫實例的 export
方法將動畫數(shù)據(jù)導(dǎo)出并傳遞給組件的 animation
屬性。
調(diào)用 export
方法后,之前的動畫操作將會被清除。
// API-DEMO page/API/animation/animation.json
{
"defaultTitle": "Animation"
}
<!-- API-DEMO page/API/animation/animation.axml-->
<view class="page">
<view class="page-description">動畫 API</view>
<view class="page-section">
<view class="page-section-title">my.createAnimation</view>
<view class="page-section-demo">
<view class="animation-element" animation="{{animation}}"></view>
</view>
<view class="page-section-btns">
<view type="primary" onTap="rotate">旋轉(zhuǎn)</view>
<view type="primary" onTap="scale"> 縮放</view>
<view type="primary" onTap="translate">移動</view>
</view>
<view class="page-section-btns">
<view type="primary" onTap="skew">傾斜</view>
<view type="primary" onTap="rotateAndScale">旋轉(zhuǎn)并縮放</view>
<view type="primary" onTap="rotateThenScale">旋轉(zhuǎn)后縮放</view>
</view>
<view class="page-section-btns">
<view type="primary" onTap="all">同時展示全部</view>
<view type="primary" onTap="allInQueue">順序展示全部</view>
<view type="primary" onTap="reset">還原</view>
</view>
</view>
</view>
// API-DEMO page/API/animation/animation.js
Page({
onReady() {
this.animation = my.createAnimation()
},
rotate() {
this.animation.rotate(Math.random() * 720 - 360).step()
this.setData({ animation: this.animation.export() })
},
scale() {
this.animation.scale(Math.random() * 2).step()
this.setData({ animation: this.animation.export() })
},
translate() {
this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
this.setData({ animation: this.animation.export() })
},
skew() {
this.animation.skew(Math.random() * 90, Math.random() * 90).step()
this.setData({ animation: this.animation.export() })
},
rotateAndScale() {
this.animation.rotate(Math.random() * 720 - 360)
.scale(Math.random() * 2)
.step()
this.setData({ animation: this.animation.export() })
},
rotateThenScale() {
this.animation.rotate(Math.random() * 720 - 360).step()
.scale(Math.random() * 2).step()
this.setData({ animation: this.animation.export() })
},
all() {
this.animation.rotate(Math.random() * 720 - 360)
.scale(Math.random() * 2)
.translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
.skew(Math.random() * 90, Math.random() * 90)
.step()
this.setData({ animation: this.animation.export() })
},
allInQueue() {
this.animation.rotate(Math.random() * 720 - 360).step()
.scale(Math.random() * 2).step()
.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
.skew(Math.random() * 90, Math.random() * 90).step()
this.setData({ animation: this.animation.export() })
},
reset() {
this.animation.rotate3d(0, 0, 0, 0)
.rotateX(0)
.rotateY(0)
.rotateZ(0)
.scale(1)
.translate(0, 0)
.skew(0, 0)
.step({ duration: 0 })
this.setData({ animation: this.animation.export() })
}
})
/* API-DEMO page/API/animation/animation.acss */
.animation-element {
width: 200rpx;
height: 200rpx;
background-color: #108ee9;
transform: scaleX(1) scaleY(1);
}
Object 類型,屬性如下:
屬性 | 類型 | 必填 | 描述 |
---|---|---|---|
duration | Integer | 否 | 動畫的持續(xù)時間,單位 ms,默認(rèn)值 400。 |
timeFunction | String | 否 | 定義動畫的效果,默認(rèn)值“l(fā)inear”,有效值:linear、ease、ease-in、ease-in-out、ease-out、step-start、step-end。 |
delay | Integer | 否 | 動畫延遲時間,單位 ms,默認(rèn)值 0。 |
transformOrigin | String | 否 | 設(shè)置 transform-origin,默認(rèn)值“50% 50% 0”。 |
示例代碼
//.js
const animation = my.createAnimation({
transformOrigin: "top right",
duration: 3000,
timeFunction: "ease-in-out",
delay: 100,
})
動畫實例可以調(diào)用以下方法來描述動畫,調(diào)用結(jié)束后會返回實例本身,支持鏈?zhǔn)秸{(diào)用的寫法。view 的 animation 屬性初始化為 {}
時,在基礎(chǔ)庫 1.11.0(不包含 1.11.0)及以下版本會報錯,建議初始化為 null
。
方法 | 參數(shù) | 說明 |
---|---|---|
opacity | value | 透明度,參數(shù)范圍 0~1。 |
backgroundColor | color | 顏色值。 |
width | length | 設(shè)置寬度:長度值,單位為 px,例如:300 px。 |
height | length | 設(shè)置高度:長度值,單位為 px,例如:300 px。 |
top | length | 設(shè)置 top 值:長度值,單位為 px,例如:300 px。 |
left | length | 設(shè)置 left 值:長度值,單位為 px,例如:300 px。 |
bottom | length | 設(shè)置 bottom 值:長度值,單位為 px,例如:300 px。 |
right | length | 設(shè)置 right 值:長度值,單位為 px,例如:300 px。 |
方法 | 參數(shù) | 說明 |
---|---|---|
rotate | deg | deg 范圍 -180 ~ 180,從原點(diǎn)順時針旋轉(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ù)時表示在 X 軸縮放 sx 倍,在 Y 軸縮放 sy 倍。 |
scaleX | sx | 在 X 軸縮放 sx 倍。 |
scaleY | sy | 在 Y 軸縮放 sy 倍。 |
scaleZ | sz | 在 Z 軸縮放 sy 倍。 |
scale3d | (sx,sy,sz) | 在 X 軸縮放 sx 倍,在 Y 軸縮放 sy 倍,在 Z 軸縮放 sz 倍。 |
方法 | 參數(shù) | 說明 |
---|---|---|
translate | tx,[ty] | 只有一個參數(shù)時,表示在 X 軸偏移 tx;兩個參數(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 軸坐標(biāo)不變,X 軸坐標(biāo)延順時針傾斜 ax 度;兩個參數(shù)時,分別在 X 軸傾斜 ax 度,在 Y 軸傾斜 ay 度。 |
skewX | ax | 參數(shù)范圍 -180 ~ 180。Y 軸坐標(biāo)不變,X 軸坐標(biāo)延順時針傾斜 ax 度。 |
skewY | ay | 參數(shù)范圍 -180~180。X 軸坐標(biāo)不變,Y 軸坐標(biāo)延順時針傾斜 ay 度。 |
方法 | 參數(shù) | 說明 |
---|---|---|
matrix | (a,b,c,d,tx,ty) | 同 transform-function。 |
matrix3d | (a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4) | 同 transform-function matrix3d。 |
step()
用于表示一組動畫完成,在一組動畫中可以調(diào)用任意多個動畫方法,一組動畫中的所有動畫會同時開始,當(dāng)一組動畫完成后才會進(jìn)行下一組動畫。step()
可以傳入一個跟 my.createAnimation()
一樣的配置參數(shù)用于指定當(dāng)前組動畫的配置。Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: