支付宝小程序API 动画·Animation

2020-09-14 14:04 更新

my.createAnimation

简介

my.createAnimation 是用于创建动画实例 animation 的 API。调用实例的方法来描述动画,最后通过动画实例的 export 方法将动画数据导出并传递给组件的 animation 属性。

使用限制

调用 export 方法后,之前的动画操作将会被清除。

扫码体验

动画.jpeg

示例代码

  1. // API-DEMO page/API/animation/animation.json
  2. {
  3. "defaultTitle": "Animation"
  4. }
  1. <!-- API-DEMO page/API/animation/animation.axml-->
  2. <view class="page">
  3. <view class="page-description">动画 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.createAnimation</view>
  6. <view class="page-section-demo">
  7. <view class="animation-element" animation="{{animation}}"></view>
  8. </view>
  9. <view class="page-section-btns">
  10. <view type="primary" onTap="rotate">旋转</view>
  11. <view type="primary" onTap="scale"> 缩放</view>
  12. <view type="primary" onTap="translate">移动</view>
  13. </view>
  14. <view class="page-section-btns">
  15. <view type="primary" onTap="skew">倾斜</view>
  16. <view type="primary" onTap="rotateAndScale">旋转并缩放</view>
  17. <view type="primary" onTap="rotateThenScale">旋转后缩放</view>
  18. </view>
  19. <view class="page-section-btns">
  20. <view type="primary" onTap="all">同时展示全部</view>
  21. <view type="primary" onTap="allInQueue">顺序展示全部</view>
  22. <view type="primary" onTap="reset">还原</view>
  23. </view>
  24. </view>
  25. </view>
  1. // API-DEMO page/API/animation/animation.js
  2. Page({
  3. onReady() {
  4. this.animation = my.createAnimation()
  5. },
  6. rotate() {
  7. this.animation.rotate(Math.random() * 720 - 360).step()
  8. this.setData({ animation: this.animation.export() })
  9. },
  10. scale() {
  11. this.animation.scale(Math.random() * 2).step()
  12. this.setData({ animation: this.animation.export() })
  13. },
  14. translate() {
  15. this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
  16. this.setData({ animation: this.animation.export() })
  17. },
  18. skew() {
  19. this.animation.skew(Math.random() * 90, Math.random() * 90).step()
  20. this.setData({ animation: this.animation.export() })
  21. },
  22. rotateAndScale() {
  23. this.animation.rotate(Math.random() * 720 - 360)
  24. .scale(Math.random() * 2)
  25. .step()
  26. this.setData({ animation: this.animation.export() })
  27. },
  28. rotateThenScale() {
  29. this.animation.rotate(Math.random() * 720 - 360).step()
  30. .scale(Math.random() * 2).step()
  31. this.setData({ animation: this.animation.export() })
  32. },
  33. all() {
  34. this.animation.rotate(Math.random() * 720 - 360)
  35. .scale(Math.random() * 2)
  36. .translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
  37. .skew(Math.random() * 90, Math.random() * 90)
  38. .step()
  39. this.setData({ animation: this.animation.export() })
  40. },
  41. allInQueue() {
  42. this.animation.rotate(Math.random() * 720 - 360).step()
  43. .scale(Math.random() * 2).step()
  44. .translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
  45. .skew(Math.random() * 90, Math.random() * 90).step()
  46. this.setData({ animation: this.animation.export() })
  47. },
  48. reset() {
  49. this.animation.rotate3d(0, 0, 0, 0)
  50. .rotateX(0)
  51. .rotateY(0)
  52. .rotateZ(0)
  53. .scale(1)
  54. .translate(0, 0)
  55. .skew(0, 0)
  56. .step({ duration: 0 })
  57. this.setData({ animation: this.animation.export() })
  58. }
  59. })
  1. /* API-DEMO page/API/animation/animation.acss */
  2. .animation-element {
  3. width: 200rpx;
  4. height: 200rpx;
  5. background-color: #108ee9;
  6. transform: scaleX(1) scaleY(1);
  7. }

入参

Object 类型,属性如下:

属性 类型 必填 描述
duration Integer 动画的持续时间,单位 ms,默认值 400。
timeFunction String 定义动画的效果,默认值“linear”,有效值:linear、ease、ease-in、ease-in-out、ease-out、step-start、step-end。
delay Integer 动画延迟时间,单位 ms,默认值 0。
transformOrigin String 设置 transform-origin,默认值“50% 50% 0”。

示例代码

  1. //.js
  2. const animation = my.createAnimation({
  3. transformOrigin: "top right",
  4. duration: 3000,
  5. timeFunction: "ease-in-out",
  6. delay: 100,
  7. })

animation

动画实例可以调用以下方法来描述动画,调用结束后会返回实例本身,支持链式调用的写法。view 的 animation 属性初始化为 {} 时,在基础库 1.11.0(不包含 1.11.0)及以下版本会报错,建议初始化为 null

样式
方法 参数 说明
opacity value 透明度,参数范围 0~1。
backgroundColor color 颜色值。
width length 设置宽度:长度值,单位为 px,例如:300 px。
height length 设置高度:长度值,单位为 px,例如:300 px。
top length 设置 top 值:长度值,单位为 px,例如:300 px。
left length 设置 left 值:长度值,单位为 px,例如:300 px。
bottom length 设置 bottom 值:长度值,单位为 px,例如:300 px。
right length 设置 right 值:长度值,单位为 px,例如:300 px。

旋转
方法 参数 说明
rotate deg deg 范围 -180 ~ 180,从原点顺时针旋转一个 deg 角度。
rotateX deg deg 范围 -180 ~ 180,在 X 轴旋转一个 deg 角度。
rotateY deg deg 范围 -180 ~ 180,在 Y 轴旋转一个 deg 角度。
rotateZ deg deg 范围 -180 ~ 180,在 Z 轴旋转一个 deg 角度。
rotate3d (x, y , z, deg) 同 transform-function rotate3d。

缩放
方法 参数 说明
scale sx,[sy] 只有一个参数时,表示在 X 轴、Y 轴同时缩放 sx 倍;两个参数时表示在 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 倍。

偏移
方法 参数 说明
translate tx,[ty] 只有一个参数时,表示在 X 轴偏移 tx;两个参数时,表示在 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。

倾斜
方法 参数 说明
skew ax,[ay] 参数范围 -180 ~ 180。只有一个参数时,Y 轴坐标不变,X 轴坐标延顺时针倾斜 ax 度;两个参数时,分别在 X 轴倾斜 ax 度,在 Y 轴倾斜 ay 度。
skewX ax 参数范围 -180 ~ 180。Y 轴坐标不变,X 轴坐标延顺时针倾斜 ax 度。
skewY ay 参数范围 -180~180。X 轴坐标不变,Y 轴坐标延顺时针倾斜 ay 度。

矩阵变形
方法 参数 说明
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() 用于表示一组动画完成,在一组动画中可以调用任意多个动画方法,一组动画中的所有动画会同时开始,当一组动画完成后才会进行下一组动画。
  • step() 可以传入一个跟 my.createAnimation() 一样的配置参数用于指定当前组动画的配置。
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号