Skip to content

t-animation

Animation 动画

组件介绍

t-animation 用于弥补 X 环境没有 uni.createAnimation 动画支持的问题,本组件支持 iOS、Android、小程序、Web、鸿蒙平台,使用简单。通过链式调用实现各种动画效果,包括旋转、缩放、平移、透明度、背景色、边距等。

平台兼容

HarmonyH5AndroidiOS小程序UTS

基础用法

基础用法参考 demo/animationins/animationins.uvue,通过 ref 获取组件实例,调用 createAnimation 方法创建动画实例,然后链式调用动画方法,最后调用 exports 执行动画。

vue
<template>
  <t-animation class="twh-200 bg-blue r-30" ref="animationRef" @transitionend="transitionend">
    <text>动画内容</text>
  </t-animation>
  
  <t-button @click="startAnimation">开始动画</t-button>
</template>

<script setup>
const animationRef = ref<ComponentPublicInstance | null>(null)

function startAnimation() {
  const ins = animationRef.value
  if (ins == null) return
  
  const ani = ins.$callMethod('createAnimation', {
    duration: 400,
    timingFunction: 'linear',
    delay: 0,
    transformOrigin: '50% 50% 0'
  })
  
  ani.scale(2, 2).rotate(45).step()
  ani.scale(1, 1).rotate(0).step()
  
  ins.$callMethod('exports')
}

function transitionend() {
  console.log('动画结束')
}
</script>

Props

属性名说明类型默认值
path点击组件后跳转的页面路径;为空时仅响应事件string''
hover是否启用点击效果booleanfalse
type组件主题类型,可选 infoprimaryerrorwarningsuccessstring''
disabled是否禁用booleanfalse
effect组件显示主题,可选 normaldarklightplainstring'normal'
size组件尺寸,可选 largemediumsmallministring'medium'

Events

事件名说明回调参数
click点击时触发UniPointerEvent
transitionend动画结束时触发-
initFinished组件初始化完成时触发,返回组件节点信息NodeInfo
touchstart触摸开始时触发-
touchend触摸结束时触发-
touchmove触摸移动时触发-
touchcancel触摸取消时触发-

Slots

插槽名说明
default动画内容区域,放置需要执行动画的元素

Methods

组件通过 defineExpose 暴露了以下实例方法:

方法名说明参数
createAnimation创建动画实例options: UTSJSONObject - 包含 durationtimingFunctiondelaytransformOrigin
exports执行动画,将动画应用到元素上-
getInfo获取组件节点信息callback: (rect: NodeInfo) => void

动画方法

创建动画实例后,可以链式调用以下动画方法:

方法名说明参数
width设置宽度value: string
height设置高度value: string
margin设置外边距value: string
marginTop设置上外边距value: string
marginBottom设置下外边距value: string
marginLeft设置左外边距value: string
marginRight设置右外边距value: string
padding设置内边距value: string
right设置右边距value: string
left设置左边距value: string
top设置上边距value: string
bottom设置下边距value: string
opacity设置透明度value: number
backgroundColor设置背景色value: string
borderColor设置边框颜色value: string
rotate旋转angle: number
rotateXX 轴旋转angle: number
rotateYY 轴旋转angle: number
rotateZZ 轴旋转angle: number
scale缩放x: number, y?: number
scaleXX 轴缩放value: number
scaleYY 轴缩放value: number
translate平移value: string
translateXX 轴平移value: string
translateYY 轴平移value: string
step结束一组动画,准备执行下一组动画options?: UTSJSONObject