微信小程序-动画

文章目录

微信小程序-动画

概述

通过调用 wx.createAnimation() 方法创建 Animation 实例。

复制代码
let animation = wx.createAnimation(Object)

再通过调用 Animation 的各方法实现动画效果。

wx.createAnimation()方法:

选项 类型 说明
duration number 动画持续事件,单位为毫秒,默认值为 400
timingFunction string 动画的效果,默认值为 linear
delay number 动画延迟执行的事件,单位为毫秒,默认值为 0
transformOrigin string 设置旋转元素的基点位置,默认值为 50% 50% 0,这3个值分别表示 x轴、y轴、z轴

timingFunction合法值:

合法值 说明
linear 动画全程线性速度
ease 动画先低速开始,然后加快,在结束前变慢
ease-in 动画以低速开始
ease-in-out 动画以低速开始和结束
ease-out 动画以低速结束
step-start 动画第一帧就跳至结束状态知道结束
step-end 动画一直保持开始状态,最后一帧跳到结束状态

Animation方法:

方法 说明
rotate(number angle) 旋转。从原点顺时针旋转一个角度,角度取值范围为 [-180,180]
export() 导出动画队列。export() 方法每次调用后会清掉之前的动画操作
scale(number sx, number sy) 缩放。当仅有 sx 参数时,表示在 X 轴、Y 轴同时缩放sx倍数,在 Y 轴缩放 sy 倍数
translate(number tx, number ty) 平移。当仅有该参数时表示在 X 轴偏移 tx,单位 px,在 Y 轴平移的距离,单位为 px
skew(number ax, number ay) 倾斜。对 X 轴坐标倾斜的角度,范围 [-180, 180],对 Y 轴坐标倾斜的角度,范围 [-180, 180]
step(Object object) 表示一组动画完成。可以在一组动画中调用任意多个动画方法,一组动画中的所有动画会同时开始,一组动画完成后才会进行下一组动画。
opacity(number value) 设置透明度,范围为 0~1
backgroundColor(string value) 设置背景色
width(number|string value) 设置宽度
top(number|string value) 设置 top 值

使用

xml 复制代码
<view class="anim-pic">
  <image src="/images/img.png" animation="{{animation}}" />
</view>
<view class="anim-btns">
  <button bind:tap="rotate">旋转</button>
  <button bind:tap="scale">缩放</button>
  <button bind:tap="translate">移动</button>
  <button bind:tap="skew">倾斜</button>
  <button bind:tap="rotateAndScale">同时旋转和缩放</button>
  <button bind:tap="rotateThenScale">先旋转后缩放</button>
  <button bind:tap="all">同时展示全部</button>
  <button bind:tap="allOrder">按顺序展示全部</button>
  <button class="reset" bind:tap="reset">重置</button>
</view>
js 复制代码
Page({
  data: {
    animation: {}
  },
  /** 旋转 */
  rotate: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 缩放 */
  scale: function () {
    this.animation.scale(Math.random() * 2).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 平移 */
  translate: function () {
    this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 倾斜 */
  skew: function () {
    this.animation.skew(Math.random() * 90, Math.random() * 90).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 同时旋转和缩放 */
  rotateAndScale: function () {
    this.animation.rotate(Math.random() * 720 - 360)
    this.animation.scale(Math.random() * 2).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 先选择后缩放 */
  rotateThenScale: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
    this.animation.scale(Math.random() * 2).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 同时旋转、缩放、平移、倾斜 */
  all: function () {
    this.animation.rotate(Math.random() * 720 - 360)
    this.animation.scale(Math.random() * 2)
    this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50)
    this.animation.skew(Math.random() * 90, Math.random() * 90).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 按顺序执行:旋转、缩放、平移、倾斜 */
  allOrder: function () {
    this.animation.rotate(Math.random() * 720 - 360).step()
    this.animation.scale(Math.random() * 2).step()
    this.animation.translate(Math.random() * 100 - 50, Math.random() * 100 - 50).step()
    this.animation.skew(Math.random() * 90, Math.random() * 90).step()
    this.setData({
      animation: this.animation.export()
    })
  },
  /** 重置 */
  reset: function () {
    this.animation.rotate(0).scale(1).translate(0, 0).skew(0, 0).step({
      duration: 0
    })
    this.setData({
      animation: this.animation.export()
    })
  },
  /
  onReady: function () {
    this.animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease"
    })
  }
})
相关推荐
2501_9159184126 分钟前
iOS 应用如何防止破解?从逆向链路还原攻击者视角,构建完整的反破解工程实践体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9160074741 分钟前
iOS 应用上架流程的工程化拆解 从签名体系到提交审核的全过程管控
android·ios·小程序·https·uni-app·iphone·webview
2501_915918411 小时前
构建可靠的 iOS 日志导出体系,从真机日志到系统行为的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
#微爱帮#1 小时前
监狱写信寄信工具评测:微爱帮 vs 寄信啦 vs 蓝信了:监狱寄信的“安全之战”背后,藏着三个残酷真相
安全·小程序·监狱写信·监狱服刑·监狱·服刑人员
桐溪漂流1 小时前
微信小程序的几个实用小知识
微信小程序·小程序·notepad++
说私域1 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的C端用户运营研究
人工智能·小程序·用户运营
wx_ywyy67981 小时前
医疗小程序合规指南:互联网医院资质、诊疗范围界定与数据安全合规
小程序·医疗小程序·小程序开发·小程序制作·医疗小程序开发·医疗小程序制作·医疗系统
2501_915909061 小时前
没有 Mac 怎么上架 iOS 应用 跨平台团队的可行交付方案分析
android·macos·ios·小程序·uni-app·iphone·webview
权泽谦1 小时前
用大语言模型实现一个离线翻译小程序(无网络也能用)
开发语言·人工智能·语言模型·小程序·php
2501_9160074712 小时前
TCP 抓包分析实战,从三次握手到自定义协议解析的完整方法
网络协议·tcp/ip·ios·小程序·uni-app·php·iphone