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