组件动画
====

在组件上创建和运行动画的快捷方式。具体用法请参考[通用方法](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-common-methods)。

#### 获取动画对象

通过调用animate方法获得animation对象，animation对象支持动画属性、动画方法和动画事件。

<!-- xxx.hml -->
<div class="container">
  <div id="content" class="box" onclick="Show"></div>
</div>

/\* xxx.css \*/
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
}
.box{
  width: 200px;
  height: 200px;
  background-color: #ff0000;
  margin-top: 30px;
}

/\* xxx.js \*/
export default {
    data: {
        animation: '',
        options: {},
        frames: {}
    },
    onInit() {
        this.options = {
            duration: 1500,
        };
        this.frames = \[
            {
                width: 200, height: 200,
            },
            {
                width: 300, height: 300,
            }
        \];
    },
    Show() {
        this.animation = this.$element('content').animate(this.frames, this.options); //获取动画对象
        this.animation.play();
    }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f2/v3/oNuC97UoSfGAy2KIDyzLeg/zh-cn_image_0000002664327985.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=3961A9387E7541E90C6D18445D105D3BA97EE13511A2B33FFEA0306C38C28486)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f6/v3/9U9vc8fuQuqhRSzc_L7VOw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=E1F57FC77DD08D5B186E3AD21C6814F895CD816035816D636C299760FED874C5)

*   使用animate方法时必须传入Keyframes和Options参数。
*   多次调用animate方法时，采用replace策略，即最后一次调用时传入的参数生效。

#### 设置动画参数

在获取动画对象后，通过设置参数Keyframes设置动画在组件上的样式。

<!-- xxx.hml -->
<div class="container">
   <div id="content" class="box" onclick="Show"></div>
</div>

/\* xxx.css \*/
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}
.box{
  width: 200px;
  height: 200px;
  background-color: #ff0000;
  margin-top: 30px;
}

/\* xxx.js \*/
export default {
  data: {
    animation: '',
    keyframes:{},
    options:{}
  },
  onInit() {
    this.options = {
      duration: 4000,
    }
    this.keyframes = \[
    {
      transform: {
        translate: '-120px -0px',
        scale: 1,
        rotate: 0
        },
        transformOrigin: '100px 100px',
        offset: 0.0,
        width: 200,
        height: 200
      },
      {
        transform: {
          translate: '120px 0px',
          scale: 1.5,
          rotate: 90
          },
          transformOrigin: '100px 100px',
          offset: 1.0,
          width: 300,
          height: 300
      }
    \]
  },
  Show() {
    this.animation = this.$element('content').animate(this.keyframes, this.options)
    this.animation.play()
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/eb/v3/6--KVgWoSSK_LE9B-26U4g/zh-cn_image_0000002633848876.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=63F13E6FC3ADD4D980C04FEC995F41CE1F003E43286A3602D5A6674C5F890DDA)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/be/v3/9XQIgtw5TY2nnnbJDqC-Kg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=B25C1DECAAE92A2B5F0AA3E6A13EB54D6AAAB3DCD52CCDBD35215A2814E9778F)

*   translate、scale和rotate的先后顺序会影响动画效果。
    
*   transformOrigin只对scale和rotate起作用。
    

在获取动画对象后，通过设置参数Options来设置动画的属性。

<!-- xxx.hml -->
<div class="container">
   <div id="content" class="box" onclick="Show"></div>
</div>

/\* xxx.css \*/
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
}
.box{
  width: 200px;
  height: 200px;
  background-color: #ff0000;
  margin-top: 30px;
}

/\* xxx.js \*/
export default {
    data: {
        animation: '',
        options: {},
        frames: {}
    },
    onInit() {
        this.options = {
            duration: 1500,
            easing: 'ease-in',
            delay: 5,
            iterations: 2,
            direction: 'normal',
        };
        this.frames = \[
            {
                transform: {
                    translate: '-150px -0px'
                }
            },
            {
                transform: {
                    translate: '150px 0px'
                }
            }
        \];
    },
    Show() {
        this.animation = this.$element('content').animate(this.frames, this.options);
        this.animation.play();
    }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/eb/v3/vSJBZOddSV2rYiNVgMtYLg/zh-cn_image_0000002634008780.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=CBC8FF426BDCED5163E279E0563125F3508D967BB663B3A2EEE0A0DF1B713A6E)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/31/v3/FWhlfts9Q0a-rUffTTBfaw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132323Z&HW-CC-Expire=86400&HW-CC-Sign=FB5F0FE8ABFD0D378319025BDF5F545522DA73EC03C3664073C93F27BD16F656)

direction：指定动画的播放模式。

normal： 动画正向循环播放。

reverse： 动画反向循环播放。

alternate：动画交替循环播放，奇数次正向播放，偶数次反向播放。

alternate-reverse：动画反向交替循环播放，奇数次反向播放，偶数次正向播放。