• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Developing Animations
2
3
4Animations are classified into [Static Animation](#static-animation) and [Continuous Animation](#continuous-animation).
5
6
7## Static Animation
8
9The transform attributes are the core of the static animation. A static animation can transform in the following three ways and only once in each way at a time:
10
11- translate: Moves a specified component horizontally or vertically.
12
13- scale: Scales a specified component horizontally or vertically to the required scale.
14
15- rotate: Rotates a specified component by a specified angle along the horizontal axis, vertical axis, or center point.
16
17For more information, see [Component Methods](../reference/arkui-js/js-components-common-methods.md). The following is an example:
18
19
20```
21<!-- xxx.hml -->
22<div class="container">
23  <text class="translate">hello</text>
24  <text class="rotate">hello</text>
25  <text class="scale">hello</text>
26</div>
27```
28
29
30```
31/* xxx.css */
32.container {
33  flex-direction: column;
34  align-items: center;
35}
36.translate {
37  height: 150px;
38  width: 300px;
39  font-size: 50px;
40  background-color: #008000;
41  transform: translate(200px);
42}
43.rotate {
44  height: 150px;
45  width: 300px;
46  font-size: 50px;
47  background-color: #008000;
48  transform-origin: 200px 100px;
49  transform: rotateX(45deg);
50}
51.scale {
52  height: 150px;
53  width: 300px;
54  font-size: 50px;
55  background-color: #008000;
56  transform: scaleX(1.5);
57}
58```
59
60
61  figure1 Static animation
62
63  ![en-us_image_0000001223127724](figures/en-us_image_0000001223127724.png)
64
65
66## Continuous Animation
67
68The static animation has only the start and end states. To set the transition state and conversion effect, use continuous animations.
69
70The core of a continuous animation is animation attributes, which define the start and end states of the animation and the curve of time and speed. Animation attributes can implement the following effects:
71
72- animation-name: Background color, opacity, width, height, and transformation type applied to the element after the animation is executed
73
74- animation-delay and animation-duration: Element delay and duration after the animation is executed
75
76- animation-timing-function: Speed curve of an animation, which makes the animation more fluent
77
78- animation-iteration-count: Number of animation playback times
79
80- animation-fill-mode: Whether to restore the initial state after the animation is executed
81
82To use the animation attributes, you need to define a @keyframes rule in the .css file, set the animation transition effect in @keyframes, and invoke the effect through a style class in the .hml file. The following is an example for animation-name:
83
84
85```
86<!-- xxx.hml -->
87<div class="item-container">
88  <text class="header">animation-name</text>
89  <div class="item {{colorParam}}">
90    <text class="txt">color</text>
91  </div>
92  <div class="item {{opacityParam}}">
93    <text class="txt">opacity</text>
94  </div>
95  <input class="button" type="button" name="" value="show" onclick="showAnimation"/>
96</div>
97```
98
99
100```
101/* xxx.css */
102.item-container {
103  margin-right: 60px;
104  margin-left: 60px;
105  flex-direction: column;
106}
107.header {
108  margin-bottom: 20px;
109}
110.item {
111  background-color: #f76160;
112}
113.txt {
114  text-align: center;
115  width: 200px;
116  height: 100px;
117}
118.button {
119  width: 200px;
120  font-size: 30px;
121  background-color: #09ba07;
122}
123.color {
124  animation-name: Color;
125  animation-duration: 8000ms;
126}
127.opacity {
128  animation-name: Opacity;
129  animation-duration: 8000ms;
130}
131@keyframes Color {
132  from {
133    background-color: #f76160;
134  }
135  to {
136    background-color: #09ba07;
137  }
138}
139@keyframes Opacity {
140  from {
141    opacity: 0.9;
142  }
143  to {
144    opacity: 0.1;
145  }
146}
147```
148
149
150```
151// xxx.js
152export default {
153  data: {
154    colorParam: '',
155    opacityParam: '',
156  },
157  showAnimation: function () {
158    this.colorParam = '';
159    this.opacityParam = '';
160    this.colorParam = 'color';
161    this.opacityParam = 'opacity';
162  },
163}
164```
165
166  figure2 Continuous animation effect
167
168  ![en-us_image_0000001223287696](figures/en-us_image_0000001223287696.gif)
169