ui-js-animate-component.md 4.9 KB
Newer Older
G
ge-yafang 已提交
1
# Component Animation
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4 5 6 7 8
Create and run an animation shortcut on the component. For details, see [Universal Methods](../reference/arkui-js/js-components-common-methods.md).


## Obtaining an Animation Object

E
esterzhou 已提交
9
Call the **animate** method to obtain an animation object, which supports animation attributes, methods, and events.
Z
zengyawen 已提交
10

E
ester.zhou 已提交
11
```html
Z
zengyawen 已提交
12 13 14 15 16 17
<!-- xxx.hml -->
<div class="container">
  <div id="content" class="box" onclick="Show"></div>
</div>
```

E
ester.zhou 已提交
18
```css
Z
zengyawen 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* 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;
}
```

E
ester.zhou 已提交
34
```js
Z
zengyawen 已提交
35 36
/* xxx.js */
export default {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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); // Obtain the animation object.
        this.animation.play();
    }
Z
zengyawen 已提交
59 60 61
}
```

G
ge-yafang 已提交
62 63
![en-us_image_0000001222807812](figures/en-us_image_0000001222807812.gif)

E
ester.zhou 已提交
64
> **NOTE**
G
ge-yafang 已提交
65 66
> - When using the animate method, you must pass the keyframes and options parameters.
> - If animate is called multiple times and the replace policy is used, parameters passed to the last call will take effect.
Z
zengyawen 已提交
67 68


G
ge-yafang 已提交
69 70 71
## Setting Animation Parameters

After obtaining an animation object, you can set its style working on the component by using the keyframes parameter.
Z
zengyawen 已提交
72

E
ester.zhou 已提交
73
```html
Z
zengyawen 已提交
74 75 76 77 78 79
<!-- xxx.hml -->
<div class="container">
   <div id="content" class="box" onclick="Show"></div>
</div>
```

E
ester.zhou 已提交
80
```css
Z
zengyawen 已提交
81 82 83 84 85 86
/* xxx.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
E
esterzhou 已提交
87
  height: 100%;
Z
zengyawen 已提交
88 89 90 91 92 93 94 95 96
}
.box{
  width: 200px;
  height: 200px;
  background-color: #ff0000;
  margin-top: 30px;
}
```

E
ester.zhou 已提交
97
```js
Z
zengyawen 已提交
98 99 100 101 102 103 104 105 106 107
/* xxx.js */
export default {
  data: {
    animation: '',
    keyframes:{},
    options:{}
  },
  onInit() {
    this.options = {
      duration: 4000,
E
esterzhou 已提交
108
    }
Z
zengyawen 已提交
109
    this.keyframes = [
E
ester.zhou 已提交
110 111 112 113 114 115 116 117 118 119 120
    {
      transform: {
        translate: '-120px -0px',   
        scale: 1,        
        rotate: 0
        },   
        transformOrigin: '100px 100px',  
        offset: 0.0, 
        width: 200,  
        height: 200   
      }, 
Z
zengyawen 已提交
121
      {
E
ester.zhou 已提交
122 123 124 125 126 127 128 129 130 131
        transform: {      
          translate: '120px 0px',     
          scale: 1.5,     
          rotate: 90   
          },
          transformOrigin: '100px 100px',
          offset: 1.0,
          width: 300,
          height: 300   
      }    
E
esterzhou 已提交
132
    ]
Z
zengyawen 已提交
133 134
  },
  Show() {
E
esterzhou 已提交
135 136
    this.animation = this.$element('content').animate(this.keyframes, this.options)
    this.animation.play()
Z
zengyawen 已提交
137 138 139 140
  }
}
```

G
ge-yafang 已提交
141
![en-us_image_0000001267647897](figures/en-us_image_0000001267647897.gif)
Z
zengyawen 已提交
142

E
ester.zhou 已提交
143
> **NOTE**
E
esterzhou 已提交
144
> - The sequence of **translate**, **scale**, and **rotate** affects the animation effect.
G
ge-yafang 已提交
145
> 
E
esterzhou 已提交
146
> - **transformOrigin** works only for scale and rotate.
Z
zengyawen 已提交
147

E
esterzhou 已提交
148
Set the animation attributes by using the **options** parameter.
Z
zengyawen 已提交
149

E
ester.zhou 已提交
150
```html
Z
zengyawen 已提交
151 152 153 154 155 156
<!-- xxx.hml -->
<div class="container">
   <div id="content" class="box" onclick="Show"></div>
</div>
```

E
ester.zhou 已提交
157
```css
Z
zengyawen 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/* 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;
}
```

E
ester.zhou 已提交
173
```js
Z
zengyawen 已提交
174 175
/* xxx.js */
export default {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    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();
    }
Z
zengyawen 已提交
206 207 208
}
```

G
ge-yafang 已提交
209
![en-us_image_0000001222967796](figures/en-us_image_0000001222967796.gif)
Z
zengyawen 已提交
210

E
ester.zhou 已提交
211 212
> **NOTE**
>
E
esterzhou 已提交
213
> **direction**: mode of playing the animation.
E
ester.zhou 已提交
214
>
E
esterzhou 已提交
215
> **normal**: plays the animation in forward loop mode.
E
ester.zhou 已提交
216
>
E
esterzhou 已提交
217
> **reverse**: plays the animation in reverse loop mode.
E
ester.zhou 已提交
218
>
E
esterzhou 已提交
219
> **alternate**: plays the animation in alternating loop mode. When the animation is played for an odd number of times, the playback is in forward direction. When the animation is played for an even number of times, the playback is in reverse direction.
E
ester.zhou 已提交
220
>
E
esterzhou 已提交
221
> **alternate-reverse**: plays the animation in reverse alternating loop mode. When the animation is played for an odd number of times, the playback is in reverse direction. When the animation is played for an even number of times, the playback is in forward direction.
Z
zengyawen 已提交
222 223