使用 动画 接口前,需要在js代码中,为element统一新增animation方法:
var animation = this.$element('xxx').animate(keyframes, options); | 限制条件 | 说明 |
|---|---|
| 适用终端 | 手机、平板、智慧屏、车机 |
| 适用区域 | 全球 |
keyframes参数说明:
| 参数 | 类型 | 说明 |
|---|---|---|
| frame | array<style> | 设置动画样式属性的多组对象。 |
style类型说明:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| width | number | - | 动画执行后应用到组件上的宽度值。 |
| height | number | - | 动画执行后应用到组件上的高度值。 |
| backgroundColor | color | none | 动画执行后应用到组件上的背景颜色。 |
| opacity | number | 1 | 动画执行后应用到组件上的不透明度值介于0到1间的数值,默认为1。 |
| transformOrigin | string | 'center center' | 定义变化过程的中心点。
|
| transform | object | - | 定义应用在元素上的变换类型,支持“transform说明”中列出的属性。 |
| backgroundPosition(1090+) | string | '0px 0px' | 背景图片在容器中绘制的位置,支持 1-4 个参数,详情请参见“通用样式”中的“background-position”说明。 |
transform说明:
| 参数 | 类型 | 说明 |
|---|---|---|
| translate/translateX/translateY/translateZ(1110+) | length | 指定元素要移动到的位置。 说明: 每个属性在keyframes数组中最多只能设置2次。 |
| translate3d(1110+) | length | percent | 3D平移,通过向量 [tx,ty,tz]来指定tx、ty和tz分别作为第一、第二和第三平移值参数。 |
| scale/scaleX/scaleY | number | 按比例放大或缩小元素。 说明: 每个属性在keyframes数组中最多只能设置2次。 |
| rotate/rotateX/rotateY | deg | 指定元素将被旋转的角度,单位是度。 说明: 每个属性在keyframes数组中最多只能设置2次。 |
options参数说明:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| duration | number | 0 | 指定动画的持续时间(单位毫秒)。默认值0,表示没有动画效果。 |
| easing | string | linear | 描述动画执行的速度曲线,用于使动画更为平滑。easing的值请参见“easing有效值说明”。 |
| delay | number | 0 | 指定请求动画操作到执行动画之间的时间间隔(单位毫秒);默认值 0,表示没有延迟。 |
| iterations | number | 1 | 动画执行次数,可设置为 Infinity 无限次。 |
| fill | none | forwards | none | 指定动画执行结束后是否恢复初始状态,默认值为none,表示恢复初始状态。 |
easing有效值说明:
| 值 | 描述 |
|---|---|
| linear | 动画从头到尾的速度是相同的。 |
| ease-in | 动画速度由慢到快。 |
| ease-out | 动画速度由快到慢。 |
| ease-in-out | 动画先加速到达中间点后减速到达终点。 |
| cubic-bezier(x1, y1, x2, y2)(1080+) | 在三次贝塞尔函数中定义变化过程,函数的x轴参数值必须处于0到1之间,y轴参数值建议处于0到1之间。 |
| steps(number-of-steps, step direction) (1090+) | 把动画分为n步阶段性展示,每段停止的时间都相同。
|
| step-start (1090+) | 等同于steps(1, jump-start) 。 |
| step-end (1090+) | 等同于steps(1, jump-end) 。 |
animation属性说明
| 属性 | 类型 | 说明 |
|---|---|---|
| finished | boolean | 只读属性,当前动画是否已完成。 |
| pending | boolean | 只读属性。当前动画是否是等待其他异步操作完成的等待状态(例如启动一个延时播放的动画)。 |
| playState | string | 获取或设置动画的执行状态。
|
| startTime | number | 获取或设置动画播放开始的预定时间,用途类似于options参数中的delay。 |
animation方法说明
| 方法 | 参数 | 说明 |
|---|---|---|
| play | - | 开始执行动画。 |
| finish | - | 结束动画。 |
| pause | - | 暂停动画。 |
| cancel | - | 取消动画。 |
| reverse | - | 反转动画的执行方向。 |
animation事件说明
| 事件 | 说明 |
|---|---|
| cancel | 动画被取消。 |
| finish | 动画执行结束。 |
示例代码
var options = {
duration: 15000,
easing: 'linear',
delay: 0,
fill: 'forwards',
iterations: 1
}
var frames = [
{transform: {
scaleX: "0.1",
scaleY: "0.1",
}},
{transform: {
scaleX: "1",
scaleY: "1",
}}];
var animation = this.$element('xxx').animate(frames, options);
animation.play();
animation.finish();
animation.pause();
animation.cancel();
animation.reverse();
animation.onfinish = function() {
console.log("animation onfinish");
}
animation.oncancel = function() {
console.log("animation oncancel");
} <template>
<div class="doc-page">
<div class="page-title-wrap">
<text class="page-title">{{componentName}}</text>
</div>
<div class="item-container">
<div class="group">
<text class="header">animation-name</text>
<div class="item {{color}}">
<text class="txt">color</text>
</div>
<div class="item {{opacity}}">
<text class="txt">opacity</text>
</div>
<div class="item {{height}}">
<text class="txt">height</text>
</div>
<div class="item {{width}}">
<text class="txt">width</text>
</div>
<div class="{{backPosition}}" style="background-image: url(../../Common/logo.png);">
<text class="txt">background-position</text>
</div>
<input class="button" type="button" value="show" onclick="show01" />
</div>
<div class="group">
<text class="header">animation-delay</text>
<div class="item {{delay}}">
<text class="txt">3s</text>
</div>
<input class="button" type="button" value="show" onclick="show02" />
</div>
<div class="group">
<text class="header">animation-duration</text>
<div class="item {{oneSec}}">
<text class="txt">1s</text>
</div>
<div class="item {{fiveSec}}">
<text class="txt">5s</text>
</div>
<div class="item {{tenSec}}">
<text class="txt">10s</text>
</div>
<input class="button" type="button" value="show" onclick="show03" />
</div>
<div class="group">
<text class="header">animation-iteration-count</text>
<div class="item {{oneCount}}">
<text class="txt">1</text>
</div>
<div class="item {{twoCount}}">
<text class="txt">2</text>
</div>
<div class="item {{threeCount}}">
<text class="txt">3</text>
</div>
<input class="button" type="button" value="show" onclick="show04" />
</div>
<div class="group">
<text class="header">animation-timing-function</text>
<div class="item {{linear}}">
<text class="txt">linear</text>
</div>
<div class="item {{ease}}">
<text class="txt">ease</text>
</div>
<div class="item {{easeIn}}">
<text class="txt">ease-in</text>
</div>
<div class="item {{easeOut}}">
<text class="txt">ease-out</text>
</div>
<div class="item {{easeInOut}}">
<text class="txt">ease-in-out</text>
</div>
<div class="item {{steps}}">
<text class="txt">steps</text>
</div>
<div class="item {{cubicBezier}}">
<text class="txt">cubic-bezier</text>
</div>
<input class="button" type="button" value="show" onclick="show05" />
</div>
<div class="group">
<text class="header">animation-fill-mode</text>
<div class="item {{forwards}}">
<text class="txt">forwards</text>
</div>
<div class="item {{none}}">
<text class="txt">none</text>
</div>
<input class="button" type="button" value="show" onclick="show06" />
</div>
<div class="group">
<text class="header">transform in animation</text>
<div class="item {{translateX}}">
<text class="txt">translateX</text>
</div>
<div class="item {{translateY}}">
<text class="txt">translateY</text>
</div>
<div class="item {{scaleX}}">
<text class="txt">scaleX</text>
</div>
<div class="item {{scaleY}}">
<text class="txt">scaleY</text>
</div>
<div class="item {{rotate}}">
<text class="txt">rotate</text>
</div>
<div class="item {{rotateX}}">
<text class="txt">rotateX</text>
</div>
<div class="item {{rotateY}}">
<text class="txt">rotateY</text>
</div>
<input class="button" type="button" value="show" onclick="show07" />
</div>
<div class="group">
<text class="header">多个动画同时播放</text>
<div class="item {{multiple}}">
<text class="txt">多个动画同时播放</text>
</div>
<input class="button" type="button" value="show" onclick="show08" />
</div>
<div class="group">
<text class="header">动画事件监听</text>
<div id="animation" class="item {{animateEvent}}">
<text class="txt">动画事件监听</text>
</div>
<input class="button" type="button" value="show" onclick="show09" />
</div>
</div>
</div>
</template>
<script>
import prompt from '@system.prompt'
export default {
private: {
componentName: 'animation',
color: '',
opacity: '',
height: '',
width: '',
delay: '',
oneSec: '',
fiveSec: '',
tenSec: '',
oneCount: '',
twoCount: '',
threeCount: '',
linear: '',
ease: '',
easeIn: '',
easeOut: '',
easeInOut: '',
forwards: '',
none: '',
translateX: '',
translateY: '',
scaleX: '',
scaleY: '',
rotate: '',
rotateX: '',
rotateY: '',
multiple: '',
animateEvent: '',
backPosition: '',
steps: '',
cubicBezier: ''
},
onInit() {
this.$page.setTitleBar({ text: 'Animation' })
},
show01() {
this.color = ''
this.opacity = ''
this.height = ''
this.width = ''
this.backPosition = ''
this.color = 'color'
this.opacity = 'opacity'
this.height = 'height'
this.width = 'width'
this.backPosition = 'backPosition'
},
show02() {
this.delay = ''
this.delay = 'delay'
},
show03() {
this.oneSec = ''
this.fiveSec = ''
this.tenSec = ''
this.oneSec = 'oneSec'
this.fiveSec = 'fiveSec'
this.tenSec = 'tenSec'
},
show04() {
this.oneCount = ''
this.twoCount = ''
this.threeCount = ''
this.oneCount = 'oneCount'
this.twoCount = 'twoCount'
this.threeCount = 'threeCount'
},
show05() {
this.linear = ''
this.ease = ''
this.easeIn = ''
this.easeOut = ''
this.easeInOut = ''
this.steps = ''
this.cubicBezier = ''
this.linear = 'linear'
this.ease = 'ease'
this.easeIn = 'easeIn'
this.easeOut = 'easeOut'
this.easeInOut = 'easeInOut'
this.steps = 'steps'
this.cubicBezier = 'cubic-bezier'
},
show06() {
this.forwards = ''
this.none = ''
this.forwards = 'forwards'
this.none = 'none'
},
show07() {
this.translateX = ''
this.translateY = ''
this.scaleX = ''
this.scaleY = ''
this.rotate = ''
this.rotateX = ''
this.rotateY = ''
this.translateX = 'translateX'
this.translateY = 'translateY'
this.scaleX = 'scaleX'
this.scaleY = 'scaleY'
this.rotate = 'rotate'
this.rotateX = 'rotateX'
this.rotateY = 'rotateY'
},
show08() {
this.multiple = ''
this.multiple = 'multiple'
},
show09() {
this.animateEvent = ''
this.animateEvent = 'animateEvent'
this.animationEvent()
},
animationEvent() {
const element = this.$element('animation')
element.addEventListener("animationstart", (event) => {
prompt.showToast({
message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
})
})
element.addEventListener("animationend", (event) => {
prompt.showToast({
message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
})
})
element.addEventListener("animationiteration", (event) => {
prompt.showToast({
message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
})
})
}
}
</script>
<style>
.doc-page {
flex: 1;
flex-direction: column;
}
.page-title-wrap {
padding-top: 50px;
padding-bottom: 80px;
justify-content: center;
}
.page-title {
padding-top: 30px;
padding-bottom: 30px;
padding-left: 40px;
padding-right: 40px;
border-color: #bbbbbb;
color: #bbbbbb;
border-bottom-width: 2px;
}
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
align-items: flex-start;
}
.group {
margin-bottom: 150px;
flex-direction: column;
align-items: flex-start;
}
.header {
margin-bottom: 20px;
}
.item {
background-color: #f76160;
}
.txt {
text-align: center;
width: 200px;
height: 100px;
}
.button {
width: 200px;
font-size: 30px;
color: #ffffff;
background-color: #09ba07;
}
.color {
animation-name: Color;
animation-duration: 8000ms;
}
.opacity {
animation-name: Opacity;
animation-duration: 8000ms;
}
.height {
animation-name: Height;
animation-duration: 8000ms;
}
.width {
animation-name: Width;
animation-duration: 1000ms;
}
.backPosition {
animation-name: BackPosition;
animation-duration: 1000ms;
}
.delay {
animation-name: Go;
animation-duration: 5000ms;
animation-delay: 3000ms;
}
.oneSec {
animation-name: Go;
animation-duration: 1000ms;
}
.fiveSec {
animation-name: Go;
animation-duration: 5000ms;
}
.tenSec {
animation-name: Go;
animation-duration: 10000ms;
}
.oneCount {
animation-name: Go;
animation-duration: 3000ms;
animation-iteration-count: 1;
}
.twoCount {
animation-name: Go;
animation-duration: 3000ms;
animation-iteration-count: 2;
}
.threeCount {
animation-name: Go;
animation-duration: 3000ms;
animation-iteration-count: 3;
}
.linear {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: linear;
}
.ease {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: ease;
}
.easeIn {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: ease-in;
}
.easeOut {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: ease-out;
}
.easeInOut {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: ease-in-out;
}
.steps {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: steps(3, end);
}
.cubic-bezier {
animation-name: Go;
animation-duration: 3000ms;
animation-timing-function: cubic-bezier(0, 0.3, 0.7, 1);
}
.forwards {
animation-name: Go;
animation-duration: 3000ms;
animation-fill-mode: forwards;
}
.none {
animation-name: Go;
animation-duration: 3000ms;
animation-fill-mode: none;
}
.translateX {
animation-name: translateX;
animation-duration: 8000ms;
}
.translateY {
animation-name: translateY;
animation-duration: 8000ms;
}
.scaleX {
animation-name: scaleX;
animation-duration: 8000ms;
}
.scaleY {
animation-name: scaleY;
animation-duration: 8000ms;
}
.rotate {
transform-origin: 0px 100px;
animation-name: rotate;
animation-duration: 8000ms;
}
.rotateX {
animation-name: rotateX;
animation-duration: 8000ms;
}
.rotateY {
animation-name: rotateY;
animation-duration: 8000ms;
}
.multiple {
animation-name: GO, Color, scaleX;
animation-duration: 3000ms;
}
.animateEvent {
animation-name: Opacity;
animation-duration: 5000ms;
animation-iteration-count: infinite;
}
@keyframes Go {
from {
background-color: #f76160;
}
to {
background-color: #09ba07;
}
}
@keyframes Color {
from {
background-color: #f76160;
}
to {
background-color: #09ba07;
}
}
@keyframes Opacity {
from {
opacity: 0.9;
}
to {
opacity: 0.1;
}
}
@keyframes Width {
from {
width: 100px;
}
to {
width: 300px;
}
}
@keyframes Height {
from {
height: 100px;
}
to {
height: 220px;
}
}
@keyframes BackPosition {
from {
background-position: left 0 top 0;
}
to {
background-position: left 100% top 100%;
}
}
@keyframes translateX {
from {
transform: translateX(20px);
}
to {
transform: translateX(50px);
}
}
@keyframes translateY {
from {
transform: translateY(20px);
}
to {
transform: translateY(50px);
}
}
@keyframes scaleX {
from {
transform: scaleX(0.5);
}
to {
transform: scaleX(2);
}
}
@keyframes scaleY {
from {
transform: scaleY(0.5);
}
to {
transform: scaleY(2);
}
}
@keyframes rotate {
from {
transform: rotate(10deg);
}
to {
transform: rotate(360deg);
}
}
@keyframes rotateX {
from {
transform: rotateX(10deg);
}
to {
transform: rotateX(360deg);
}
}
@keyframes rotateY {
from {
transform: rotateY(10deg);
}
to {
transform: rotateY(360deg);
}
}
</style> 效果图如下:
