微信小程序-动画

微信小程序-动画

概述

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

let animation = wx.createAnimation(Object)

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

wx.createAnimation()方法:

选项类型说明
durationnumber动画持续事件,单位为毫秒,默认值为 400
timingFunctionstring动画的效果,默认值为 linear
delaynumber动画延迟执行的事件,单位为毫秒,默认值为 0
transformOriginstring设置旋转元素的基点位置,默认值为 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 值

使用

<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>
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"
    })
  }
})
### 微信小程序电子请柬开发教程及相关资料 #### 资料概述 微信小程序作为一种轻量级的应用形式,非常适合用于制作电子请柬。这类应用通常涉及页面设计、动画效果以及数据交互等功能[^1]。以下是关于如何开发微信小程序电子请柬的一些关键知识点和示例代码。 --- #### 技术栈准备 在开发之前,需要熟悉以下几个关键技术点: - **WXML (WeiXin Markup Language)**:定义小程序的结构。 - **WXSS (WeiXin Style Sheets)**:负责样式布局。 - **JavaScript**:实现逻辑功能。 - **JSON**:配置文件,控制页面属性。 可以通过官方文档或其他学习平台获取基础技能支持[^1]。 --- #### 功能模块分析 一个典型的电子请柬可能包括以下功能模块: 1. **封面页**:展示主题图片和背景音乐播放按钮。 2. **正文内容**:显示邀请函的具体文字描述。 3. **互动区域**:提供 RSVP(回复确认)表单或留言区。 4. **分享功能**:允许用户通过微信好友或朋友圈分享。 这些模块可以分别由不同的 WXML 页面组成,并通过 `app.json` 文件中的路由设置连接起来。 --- #### 示例代码片段 以下是一个简单的电子请柬首页模板: ```html <!-- index.wxml --> <view class="container"> <!-- 封面图 --> <image src="/images/cover.jpg" mode="aspectFill"></image> <!-- 音乐播放器 --> <audio id="myAudio" src="/music/background.mp3" controls loop></audio> <!-- 进入详情按钮 --> <button bindtap="navigateToDetails">进入请柬</button> </view> ``` ```css /* index.wxss */ .container { display: flex; flex-direction: column; align-items: center; } image { width: 100%; height: auto; } ``` ```javascript // index.js Page({ navigateToDetails() { wx.navigateTo({ url: '/pages/details/details', }); }, }); ``` 此代码展示了基本的 UI 结构和跳转逻辑[^1]。 如果希望引入第三方库来增强用户体验(如 jQuery),可以在 Spring Boot 后端项目中集成 WebJars 来管理前端依赖项[^2]。不过需要注意的是,在纯小程序环境中并不直接支持此类工具链。 --- #### 学习资源推荐 对于初学者来说,可以从一些公开的学习路径入手,比如参考专门针对小程序开发者提供的原创课程或者案例解析文章[^1]。此外还可以关注社区论坛上的讨论帖寻找灵感。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值