• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Component Animation
2
3
4Create and run an animation shortcut on the component. For details, see [Universal Methods](../reference/arkui-js/js-components-common-methods.md).
5
6
7## Obtaining an Animation Object
8
9Call the animate method to obtain an animation object, which supports animation attributes, methods, and events.
10
11
12```
13<!-- xxx.hml -->
14<div class="container">
15  <div id="content" class="box" onclick="Show"></div>
16</div>
17```
18
19
20```
21/* xxx.css */
22.container {
23  flex-direction: column;
24  justify-content: center;
25  align-items: center;
26  width: 100%;
27}
28.box{
29  width: 200px;
30  height: 200px;
31  background-color: #ff0000;
32  margin-top: 30px;
33}
34```
35
36
37```
38/* xxx.js */
39export default {
40  data: {
41    animation: '',
42  },
43  onInit() {
44  },
45  onShow() {
46    var options = {
47      duration: 1500,
48    };
49    var frames = [
50      {
51        width:200,height:200,
52      },
53      {
54        width:300,height:300,
55      }
56    ];
57    this.animation = this.$element('content').animate(frames, options);  // Obtain the animation object.
58  },
59  Show() {
60    this.animation.play();
61  }
62}
63```
64
65![en-us_image_0000001222807812](figures/en-us_image_0000001222807812.gif)
66
67> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
68> - When using the animate method, you must pass the keyframes and options parameters.
69> - If animate is called multiple times and the replace policy is used, parameters passed to the last call will take effect.
70
71
72## Setting Animation Parameters
73
74After obtaining an animation object, you can set its style working on the component by using the keyframes parameter.
75
76
77```
78<!-- xxx.hml -->
79<div class="container">
80   <div id="content" class="box" onclick="Show"></div>
81</div>
82```
83
84
85```
86/* xxx.css */
87.container {
88  flex-direction: column;
89  justify-content: center;
90  align-items: center;
91  width: 100%;
92}
93.box{
94  width: 200px;
95  height: 200px;
96  background-color: #ff0000;
97  margin-top: 30px;
98}
99```
100
101
102```
103/* xxx.js */
104export default {
105  data: {
106    animation: '',
107    keyframes:{},
108    options:{}
109  },
110  onInit() {
111    this.options = {
112      duration: 4000,
113    };
114    this.keyframes = [
115      {
116        transform: {
117          translate: '-120px -0px',
118          scale: 1,
119          rotate: 0
120        },
121        transformOrigin: '100px 100px',
122        offset: 0.0,
123        width: 200,
124        height: 200
125      },
126      {
127        transform: {
128          translate: '120px 0px',
129          scale: 1.5,
130          rotate: 90
131        },
132        transformOrigin: '100px 100px',
133        offset: 1.0,
134        width: 300,
135        height: 300
136      }
137    ];
138  },
139  Show() {
140    this.animation = this.$element('content').animate(this.keyframes, this.options);
141    this.animation.play();
142  }
143}
144```
145
146![en-us_image_0000001267647897](figures/en-us_image_0000001267647897.gif)
147
148> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
149> - The sequence of translate, scale, and rotate affects the animation effect.
150>
151> - transformOrigin works only for scale and rotate.
152
153Set the animation attributes by using the options parameter.
154
155
156```
157<!-- xxx.hml -->
158<div class="container">
159   <div id="content" class="box" onclick="Show"></div>
160</div>
161```
162
163
164```
165/* xxx.css */
166.container {
167  flex-direction: column;
168  justify-content: center;
169  align-items: center;
170  width: 100%;
171}
172.box{
173  width: 200px;
174  height: 200px;
175  background-color: #ff0000;
176  margin-top: 30px;
177}
178```
179
180
181```
182/* xxx.js */
183export default {
184  data: {
185    animation: '',
186  },
187  onInit() {
188  },
189  onShow() {
190    var options = {      duration: 1500,      easing: 'ease-in',      delay: 5,      iterations: 2,      direction: 'normal',    };
191    var frames = [
192      {
193        transform: {
194          translate: '-150px -0px'
195        }
196      },
197      {
198        transform: {
199          translate: '150px 0px'
200        }
201      }
202    ];
203    this.animation = this.$element('content').animate(frames, options);
204  },
205  Show() {
206    this.animation.play();
207  }
208}
209```
210
211![en-us_image_0000001222967796](figures/en-us_image_0000001222967796.gif)
212
213> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
214> direction: mode of playing the animation.
215>
216> normal: plays the animation in forward loop mode.
217>
218> reverse: plays the animation in reverse loop mode.
219>
220> 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.
221>
222> 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.
223
224
225## Adding an Event and Calling a Method
226
227Animation objects support animation events and methods. You can achieve the intended animation by adding start and cancel events and calling the play, pause, rewind, and stop methods.
228
229
230```
231<!-- xxx.hml -->
232<div class="container">
233 <div id="content" style="width: 350px;height: 350px;margin-top: 100px;background: linear-gradient(pink, purple);">
234 </div>
235   <div class="row">
236      <button type="capsule" value="play" onclick="playAnimation"></button>
237      <button type="capsule" value="pause" onclick="pauseAnimation"></button>
238   </div>
239   <div class="row1">
240      <button type="capsule" value="reverse" onclick="reverseAnimation"></button>
241      <button type="capsule" value="cancel" onclick="cancelAnimation"></button>
242   </div>
243</div>
244```
245
246
247```
248/* xxx.css */
249.container {
250  flex-direction: column;
251  align-items: center;
252  justify-content: center;
253}
254button{
255  width: 200px;
256}
257.row{
258  width: 65%;
259  height: 100px;
260  align-items: center;
261  justify-content: space-between;
262  margin-top: 40px;
263  position: fixed;
264  top: 65%;
265  left: 120px;
266}
267.row1{
268  width: 65%;
269  height: 100px;
270  align-items: center;
271  justify-content: space-between;
272  margin-top: 30px;
273  position: fixed;
274  top: 75%;
275  left: 120px;
276}
277```
278
279
280```
281/* xxx.js */
282import prompt from '@system.prompt';
283export default {
284  data: {
285    animation: '',
286  },
287  onInit() {
288  },
289  onShow() {
290    var options = {
291      duration: 1500,
292      easing:'ease-in',
293      elay:5,
294      direction:'normal',
295      iterations:2
296    };
297    var frames = [
298      {
299        transform: {
300          translate: '-150px -0px'
301        },
302        opacity: 0.1,
303        offset: 0.0,
304        width: 200,
305        height: 200,
306      },
307      {
308        transform: {
309          translate: '150px 0px'
310        },
311        opacity: 1.0,
312        offset: 1.0,
313        width: 300,
314        height: 300,
315      }
316    ];
317    this.animation = this.$element('content').animate(frames, options);
318    this.animation.onstart = function(){
319      prompt.showToast({
320        message: "start"
321      });
322    }; // Add a start event.
323    this.animation.onrepeat = function(){
324      prompt.showToast({
325        message: " repeated"
326      });
327    };// Add a repeat event.
328    this.animation.oncancel = function(){
329      prompt.showToast({
330        message: "canceled"
331      });
332    };// Add a cancellation event.
333    this.animation.onfinish = function(){
334      prompt.showToast({
335        message: "finish"
336      });
337    };// Add a finish event.
338  },
339  playAnimation() {
340    this.animation.play();// Start this animation.
341  },
342  pauseAnimation() {
343    this.animation.pause();// Pause this animation.
344  },
345  reverseAnimation() {
346    this.animation.reverse();// Reverse this animation.
347  },
348  cancelAnimation() {
349    this.animation.cancel();// Cancel this animation.
350  }
351}
352```
353
354![en-us_image_0000001223127752](figures/en-us_image_0000001223127752.gif)
355
356Change the animation status by changing the playStat attribute.
357
358
359```
360<!-- xxx.hml -->
361<div class="container">
362  <div id="content" style="width: 350px;height: 350px;margin-top: 100px;background: linear-gradient(pink, purple);">
363  </div>
364  <div class="row">
365     <button type="capsule" value="{{state}}" onclick="playStateClick"></button>
366  </div>
367  <div class="row1">
368     <button type="capsule" value="{{state1}}" onclick="playStateClick1"></button>
369  </div>
370</div>
371```
372
373
374```
375/* xxx.css */
376.container {
377  flex-direction: column;
378  align-items: center;
379  justify-content: center;
380}
381button{
382  width: 200px;
383}
384.row{
385  width: 65%;
386  height: 100px;
387  align-items: center;
388  justify-content: space-between;
389  margin-top: 50px;
390  margin-left: 260px;
391  position: fixed;
392  top: 65%;
393}
394.row1{
395  width: 65%;
396  height: 100px;
397  align-items: center;
398  justify-content: space-between;
399  margin-top: 50px;
400   margin-left: 260px;
401  position: fixed;
402  top: 75%;
403}
404```
405
406
407```
408/* xxx.js */
409import prompt from '@system.prompt';
410export default {
411  data: {
412    animation: '',
413    state:'play',
414    state1:'play'
415  },
416  onInit() {
417  },
418  onShow() {
419    var options = {
420      duration: 1500,
421      easing:'ease-in',
422      elay:5,
423      direction:'normal',
424      iterations:2,
425    };
426    var frames = [
427      {
428        transform: {
429          translate: '-150px -0px'
430        },
431        opacity: 0.1,
432        offset: 0.0,
433        width: 200,
434        height: 200,
435      },
436      {
437        transform: {
438          translate: '150px 0px'
439        },
440          opacity: 1.0,
441          offset: 1.0,
442          width: 300,
443          height: 300,
444        }
445      ];
446      this.animation = this.$element('content').animate(frames, options);
447      this.animation.onstart = function(){
448        prompt.showToast({
449          message: "start"
450        });
451      };
452      this.animation.onrepeat = function(){
453        prompt.showToast({
454          message: " repeated"
455        });
456      };
457      this.animation.onfinish = function(){
458        prompt.showToast({
459          message: " finished"
460      });
461    };
462  },
463  playStateClick(){
464    if(this.animation.playState != 'running'){
465      this.animation.playState = 'running';// Set playState to running to run the animation.
466      this.state = 'pause'
467    }else{
468      this.animation.playState = 'paused';// Set playState to paused to pause the animation.
469      this.state = 'play'
470    }
471  },
472  playStateClick1(){
473    if(this.animation.playState != 'running'){
474      this.animation.playState = 'running';
475      this.state1 = 'finish'
476    }else{
477      this.animation.playState = 'finished';// Set playState to finished to stop the animation.
478      this.state1 = 'play'
479    }
480  }
481}
482```
483
484![en-us_image_0000001267607921](figures/en-us_image_0000001267607921.gif)
485