W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:創(chuàng)建一個(gè)動(dòng)畫實(shí)例 animation
Object object
Animation
屬性名 | 類型 | 必填 | 默認(rèn)值 | 說明 |
---|---|---|---|---|
duration | Number | 否 | 400 | 動(dòng)畫持續(xù)時(shí)間,單位 ms 。 |
timingFunction | String | 否 |
| 定義動(dòng)畫的效果 |
delay | Number | 否 | 0 | 動(dòng)畫延遲時(shí)間,單位 ms 。 |
transformOrigin | String | 否 |
| 動(dòng)畫 |
值 | 說明 |
---|---|
linear | 以相同速度開始至結(jié)束。 |
ease | 慢速開始,然后變快,然后慢速結(jié)束。 |
ease-in | 慢速開始的過渡效果。 |
ease-in-out | 動(dòng)畫以低速開始和結(jié)束。 |
ease-out | 動(dòng)畫以低速結(jié)束。 |
step-start | 動(dòng)畫第一幀就跳至結(jié)束狀態(tài)直到結(jié)束。 |
step-end | 動(dòng)畫一直保持開始狀態(tài),最后一幀跳到結(jié)束狀態(tài)。 |
代碼示例 1:
<view class="wrap">
<view class="animation-element-wrapper flexTop">
<view class="animation-element" animation="{{animation}}"></view>
</view>
<view class="card-area flexBottom swan-security-padding-bottom" style="height:{{isIPhoneX ? 5.7 : 4.1}}rem">
<view class="top-description border-bottom">展示動(dòng)畫</view>
<scroll-view scroll-y class="scroll">
<button type="primary" bindtap="rotate">旋轉(zhuǎn)</button>
<button type="primary" bindtap="scale">縮放</button>
<button type="primary" bindtap="translate">移動(dòng)</button>
<button type="primary" bindtap="skew">傾斜</button>
<button type="primary" bindtap="rotateAndScale">旋轉(zhuǎn)并縮放</button>
<button type="primary" bindtap="rotateThenScale">旋轉(zhuǎn)后縮放</button>
<button type="primary" bindtap="all">同時(shí)展示全部動(dòng)作</button>
<button type="primary" bindtap="allInQueue">順序展示全部動(dòng)作</button>
<button bindtap="reset">還原</button>
</scroll-view>
</view>
</view>
Page({
data: {
isIPhoneX: false,
animation: {}
},
onLoad() {
this.animation = swan.createAnimation({
duration: 1000,
timingFunction: 'ease',
delay: 0,
transformOrigin: '50% 50% 0'
});
swan.getSystemInfo({
success: systemInfo => {
// 針對(duì)適配某一機(jī)型和模擬器
if (systemInfo.model
&& (systemInfo.model.indexOf('iPhone X') > -1)
|| (systemInfo.model === 'iPhone Simulator <x86-64>'
&& systemInfo.screenWidth === 375)
) {
this.setData({
isIPhoneX: true
});
}
}
});
},
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.rotate(0, 0)
.scale(1)
.translate(0, 0)
.skew(0, 0)
.step({
duration: 0
});
this.setData({
animation: this.animation.export()
});
}
});
.animation-element-wrapper {
display: flex;
overflow: hidden;
height: 2.18rem;
margin: .13rem .17rem;
border-radius: 8px 8px;
background-color: #fff;
}
.flexTop {
position: fixed;
height: 2.18rem;
width: 3.88rem;
height: 2.18rem;
}
.animation-element {
width: .6rem;
height: .6rem;
margin-top: .7rem;
margin-left: 1.6rem;
background-color: #2b99ff;
}
.flexBottom {
position: fixed;
bottom: 0;
width: 3.88rem;
}
.scroll {
height: 3.5rem;
}
代碼示例 2: 參數(shù)默認(rèn)值
<view class="wrap">
<view class="anim" bindtap="startToAnimate" animation="{{animationData}}"></view>
</view>
Page({
data: {
animationData: {}
},
startToAnimate() {
const animation = swan.createAnimation();
animation.rotate(90).translateY(10).step();
this.setData({
animationData: animation.export()
});
console.log('createAnimation', animation);
}
});
代碼示例 3: timingFunction
<view class="wrap">
<view class="anim" bindtap="startToAnimate" animation="{{animationData}}"></view>
</view>
Page({
data: {
animationData: {}
},
startToAnimate() {
const animation = swan.createAnimation({
transformOrigin: '50% 50%',
duration: 1000,
// timingFunction可選值還有 ease,ease-in,ease-in-out,ease-out,step-start,step-end
timingFunction: 'linear',
delay: 0
});
animation.rotate(90).translateY(10).step();
this.setData({
animationData: animation.export()
});
console.log('createAnimation', animation);
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: