1e41f4b71Sopenharmony_ci# Animation Styles 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **NOTE** 4e41f4b71Sopenharmony_ci> 5e41f4b71Sopenharmony_ci> The initial APIs of this component are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciComponents support dynamic rotation, translation, and scaling effects. These effects can be set in the **style** attribute or CSS files. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci| Name | Type | Description | 10e41f4b71Sopenharmony_ci| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 11e41f4b71Sopenharmony_ci| transform-origin | string<sup>6+</sup> \| \<percentage> \| \<length> string<sup>6+</sup> \| \<percentage> \| \<length> | Origin position of the transformed element. The unit can be px or a percentage (relative to the animation target component). If only one value is specified, the other one is **50%**. The available values for the first string are **left**, **center**, and **right**. The available values for the second string are **top**, **center**, and **bottom**.<br>Example:<br/>transform-origin: 200px 30%<br/>transform-origin: 100px topt<br/>ransform-origin: center center<br/>Default value: **center center** | 12e41f4b71Sopenharmony_ci| transform | string | Translation, rotation, and scaling attributes.<br/>For details, see **transform**. | 13e41f4b71Sopenharmony_ci| animation<sup>6+</sup> | string | Animation attributes in the format of **duration \| timing-function \| delay \| iteration-count \| direction \| fill-mode \| play-state \| name**. The order of the parameters is not specified, but the **duration** and **delay** parameters are parsed based on where they are placed.<br/>Default value: 0s ease 0s 1 normal none running none | 14e41f4b71Sopenharmony_ci| animation-name | string | @keyframes rule. For details, see **@keyframes**. | 15e41f4b71Sopenharmony_ci| animation-delay | \<time> | Delay for playing the animation, in ms or s, for example, **1000 ms** or **1s**. The default unit is ms.<br/>Default value: 0 | 16e41f4b71Sopenharmony_ci| animation-duration | \<time> | Animation duration, in ms or s, for example, **1000 ms** or **1s**. The default unit is ms.<br/>**NOTE**<br/>**animation-duration** must be specified. Otherwise, the duration is **0**, which means the animation will not be played.<br/>Default value: **0** | 17e41f4b71Sopenharmony_ci| animation-iteration-count | number \| infinite | Number of times that an animation is played. The animation is played once by default. You can set the value to **infinite** to play the animation infinitely.<br/>Default value: **1** | 18e41f4b71Sopenharmony_ci| animation-timing-function | string | Speed curve of an animation, which makes the animation more fluent.Available values are as follows: <br>- **linear**: The animation speed keeps unchanged. <br>- **ease**: The animation starts slowly, accelerates, and then slows down towards the end. The cubic-bezier curve (0.25, 0.1, 0.25, 1.0) is used.<br>- **ease-in**: The animation starts at a low speed. The cubic-bezier curve (0.42, 0.0, 1.0, 1.0) is used.<br>- **ease-out**: The animation ends at a low speed. The cubic-bezier curve (0.0, 0.0, 0.58, 1.0) is used.<br>- **ease-in-out**: The animation starts and ends at a low speed. The cubic-bezier curve (0.42, 0.0, 0.58, 1.0) is used.<br>- **friction**: The animation uses the friction cubic-bezier curve (0.2, 0.0, 0.2, 1.0).<br>- **extreme-deceleration**: The animation uses the extreme deceleration cubic-bezier curve (0.0, 0.0, 0.0, 1.0).<br>- **sharp**: The animation uses the sharp cubic-bezier curve (0.33, 0.0, 0.67, 1.0).<br>- **rhythm**: The animation uses the rhythm cubic-bezier curve (0.7, 0.0, 0.2, 1.0).<br>- **smooth**: The animation uses the smooth cubic-bezier curve (0.4, 0.0, 0.4, 1.0).<br>- **cubic-bezier**: You can customize an animation speed curve in the **cubic-bezier()** function. The x and y values of each input parameter must be between 0 and 1.<br>- **steps**: The animation uses the step curve<sup>6+</sup>. The syntax is as follows: steps(number[, end\|start]), where the first parameter **number** is mandatory and must be a positive integer; the second parameter is optional and indicates that the start point or end point (default) of each interval changes step by step.<br/>Default value: **ease** | 19e41f4b71Sopenharmony_ci| animation-direction<sup>6+</sup> | string | Mode of playing the animation. <br>- **normal**: Plays the animation in forward loop mode. <br>- **reverse**: Plays the animation in reverse loop mode. <br>- **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 backward direction. <br>- **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 backward direction. When the animation is played for an even number of times, the playback is in forward direction.<br/>Default value: **normal** | 20e41f4b71Sopenharmony_ci| animation-fill-mode | string | Start and end styles of the animation <br>- **none**: No style is applied to the target before or after the animation is executed. <br>- **forwards**: The target keeps the state at the end of the animation (defined in the last key frame) after the animation is executed. <br>- **backwards**<sup>6+</sup>: The animation uses the value defined in the first key frame during the **animation-delay**. When **animation-direction** is set to **normal** or **alternate**, the value in the **from** key frame is used. When **animation-direction** is set to **reverse** or **alternate-reverse**, the value in the **to** key frame is used.. <br>- **both**<sup>6+</sup>: The animation follows the **forwards** and **backwards** rules.<br/>Default value: none | 21e41f4b71Sopenharmony_ci| animation-play-state<sup>6+</sup> | string | Current state of the animation. <br>- **paused**: paused <br>- **running**: playing<br/>Default value: **running** | 22e41f4b71Sopenharmony_ci| transition<sup>6+</sup> | string | Transition effect when the component status is switched. The following four attributes can be set through the **transition** attribute.<br>- **transition-property**: name of the CSS property for setting the transition effect. Currently, the width, height, and background color are supported. <br>- **transition-duration**: duration required for completing the transition effect, in seconds. <br>- **transition-timing-function**: time curve of the transition effect. The curve provided by the style animation is supported. <br>- **transition-delay**: delay for starting the transition effect, in seconds.<br/>Default value: **all 0 ease 0** | 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**Table 1** transform 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci| Name | Type | Description | 27e41f4b71Sopenharmony_ci| ------------- | -------------------------------------- | ------------------------------------------------------------ | 28e41f4b71Sopenharmony_ci| none<sup>6+</sup> | - | Does not perform any transformation. | 29e41f4b71Sopenharmony_ci| matrix<sup>6+</sup> | \<number> | Defines a matrix of six values, which represent scaleX, skewY, skewX, scaleY, translateX, translateY. | 30e41f4b71Sopenharmony_ci| matrix3d<sup>6+</sup> | \<number> | Defines a 4 x 4 matrix of 16 values. | 31e41f4b71Sopenharmony_ci| translate | \<length> \| \<percent> | Moves an element in 2D space. | 32e41f4b71Sopenharmony_ci| translate3d<sup>6+</sup> | \<length> \| \<percent> | Moves an element in 3D space. There are three input parameters, indicating the translation distance of the x-axis, y-axis, and z-axis, respectively. | 33e41f4b71Sopenharmony_ci| translateX | \<length> \| \<percent> | Moves an element along the x-axis. | 34e41f4b71Sopenharmony_ci| translateY | \<length> \| \<percent> | Moves an element along the y-axis. | 35e41f4b71Sopenharmony_ci| translateZ<sup>6+</sup> | \<length> \| \<percent> | Moves an element along the z-axis. | 36e41f4b71Sopenharmony_ci| scale | \<number> | Resizes an element in 2D space. You need to set the target width and height on the x-axis and y-axis. | 37e41f4b71Sopenharmony_ci| scale3d<sup>6+</sup> | \<number> | Resizes an element in 3D space. There are three input parameters, indicating the scale ratio of the x-axis, y-axis, and z-axis, respectively. | 38e41f4b71Sopenharmony_ci| scaleX | \<number> | Resizes an element along the x-axis. | 39e41f4b71Sopenharmony_ci| scaleY | \<number> | Resizes an element along the y-axis. | 40e41f4b71Sopenharmony_ci| scaleZ<sup>6+</sup> | \<number> | Resizes an element along the z-axis. | 41e41f4b71Sopenharmony_ci| rotate | \<deg> \| \<rad> \| \<grad><sup>6+</sup> \| \<turn><sup>6+</sup> | Rotates an element in 2D space. You can set the element to rotate around its x-axis or y-axis. | 42e41f4b71Sopenharmony_ci| rotate3d<sup>6+</sup> | \<deg> \| \<rad> \| \<grad> \| \<turn> | Rotates an element in 3D space. There are four input parameters: The first three parameters are the rotation vectors of the x-axis, y-axis, and z-axis, and the fourth parameter is the rotation angle. | 43e41f4b71Sopenharmony_ci| rotateX | \<deg> \| \<rad> \| \<grad><sup>6+</sup> \| \<turn><sup>6+</sup> | Rotates an element around the x-axis. | 44e41f4b71Sopenharmony_ci| rotateY | \<deg> \| \<rad> \| \<grad><sup>6+</sup> \| \<turn><sup>6+</sup> | Rotates an element around the y-axis. | 45e41f4b71Sopenharmony_ci| rotateZ<sup>6+</sup> | \<deg> \| \<rad> \| \<grad> \| \<turn> | Rotates an element around the z-axis. | 46e41f4b71Sopenharmony_ci| skew<sup>6+</sup> | \<deg> \| \<rad> \| \<grad> \| \<turn> | Skews an element in 2D space. There are two input parameters, indicating the 2D skew angle of the x-axis and y-axis, respectively. | 47e41f4b71Sopenharmony_ci| skewX<sup>6+</sup> | \<deg> \| \<rad> \| \<grad> \| \<turn> | Skews an element along the x-axis in 2D space. | 48e41f4b71Sopenharmony_ci| skewY<sup>6+</sup> | \<deg> \| \<rad> \| \<grad> \| \<turn> | Skews an element along the y-axis in 2D space. | 49e41f4b71Sopenharmony_ci| perspective<sup>6+</sup> | \<number> | Sets the distance from the lens to the element's surface in the 3D perspective scenario. | 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**Table 2** @keyframes 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci| Name | Type | Default Value | Description | 54e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------------------------ | ------------- | ------------------------------------------------------------ | 55e41f4b71Sopenharmony_ci| background-color | \<color> | - | Background color applied to the component after the animation is played. | 56e41f4b71Sopenharmony_ci| opacity | number | 1 | Opacity value applied to the component after the animation is played. The value ranges from **0** to **1**. The default value is **1**. | 57e41f4b71Sopenharmony_ci| width | \<length> | - | Width value applied to the component after the animation is played. | 58e41f4b71Sopenharmony_ci| height | \<length> | - | Height value applied to the component after the animation is played. | 59e41f4b71Sopenharmony_ci| transform | string | - | Transformation type applied to the component. For details, see **transform**. | 60e41f4b71Sopenharmony_ci| background-position<sup>6+</sup> | string \| \<percentage> \| \<length> string \| \<percentage> \| \<length> | 50% 50% | Position of the background image. The unit can be percentage or px. The first value indicates the horizontal position, and the second value indicates the vertical position. If only one value is specified, the other one is **50%**. The available values for the first string are **left**, **center**, and **right**. The available values for the second string are **top**, **center**, and **bottom**.<br>Example:<br>- background-position: 200px 30%<br>- background-position: 100px top<br>- background-position: center center | 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciIf there is no default value for when an animation will start or end, use **from** and **to** to specify the start and end of the display. You can use a percentage value to specify an intermediate state of the animation.<sup>6+</sup> The following is an example: 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci```html 65e41f4b71Sopenharmony_ci<!-- xxx.hml --> 66e41f4b71Sopenharmony_ci<div class="container"> 67e41f4b71Sopenharmony_ci <div class="rect"> 68e41f4b71Sopenharmony_ci </div> 69e41f4b71Sopenharmony_ci</div> 70e41f4b71Sopenharmony_ci``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci```css 73e41f4b71Sopenharmony_ci/* xxx.css */ 74e41f4b71Sopenharmony_ci.container { 75e41f4b71Sopenharmony_ci display: flex; 76e41f4b71Sopenharmony_ci justify-content: center; 77e41f4b71Sopenharmony_ci align-items: center; 78e41f4b71Sopenharmony_ci margin: 150px; 79e41f4b71Sopenharmony_ci} 80e41f4b71Sopenharmony_ci.rect{ 81e41f4b71Sopenharmony_ci width: 200px; 82e41f4b71Sopenharmony_ci height: 200px; 83e41f4b71Sopenharmony_ci background-color: #f76160; 84e41f4b71Sopenharmony_ci animation: Go 3s infinite; 85e41f4b71Sopenharmony_ci} 86e41f4b71Sopenharmony_ci@keyframes Go 87e41f4b71Sopenharmony_ci{ 88e41f4b71Sopenharmony_ci from { 89e41f4b71Sopenharmony_ci background-color: #f76160; 90e41f4b71Sopenharmony_ci transform:translate(100px) rotate(0deg) scale(1.0); 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci /* Specify the intermediate state of the animation by a percentage value.<sup>6+</sup> */ 93e41f4b71Sopenharmony_ci 50% { 94e41f4b71Sopenharmony_ci background-color: #f76160; 95e41f4b71Sopenharmony_ci transform:translate(100px) rotate(60deg) scale(1.3); 96e41f4b71Sopenharmony_ci } 97e41f4b71Sopenharmony_ci to { 98e41f4b71Sopenharmony_ci background-color: #09ba07; 99e41f4b71Sopenharmony_ci transform:translate(100px) rotate(180deg) scale(2.0); 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci} 102e41f4b71Sopenharmony_ci``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci```html 107e41f4b71Sopenharmony_ci<!-- xxx.hml --> 108e41f4b71Sopenharmony_ci<div class="container"> 109e41f4b71Sopenharmony_ci <div class="simpleAnimation simpleSize" style="animation-play-state: {{playState}}"></div> 110e41f4b71Sopenharmony_ci <text onclick="toggleState">animation-play-state: {{playState}}</text> 111e41f4b71Sopenharmony_ci</div> 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci```css 115e41f4b71Sopenharmony_ci/* xxx.css */ 116e41f4b71Sopenharmony_ci.container { 117e41f4b71Sopenharmony_ci flex-direction: column; 118e41f4b71Sopenharmony_ci justify-content: center; 119e41f4b71Sopenharmony_ci align-items: center; 120e41f4b71Sopenharmony_ci} 121e41f4b71Sopenharmony_ci.simpleSize { 122e41f4b71Sopenharmony_ci background-color: blue; 123e41f4b71Sopenharmony_ci width: 100px; 124e41f4b71Sopenharmony_ci height: 100px; 125e41f4b71Sopenharmony_ci} 126e41f4b71Sopenharmony_ci.simpleAnimation { 127e41f4b71Sopenharmony_ci animation: simpleFrames 9s; 128e41f4b71Sopenharmony_ci} 129e41f4b71Sopenharmony_ci@keyframes simpleFrames { 130e41f4b71Sopenharmony_ci from { transform: translateX(0px); } 131e41f4b71Sopenharmony_ci to { transform: translateX(100px); } 132e41f4b71Sopenharmony_ci} 133e41f4b71Sopenharmony_ci``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci```js 136e41f4b71Sopenharmony_ci// xxx.js 137e41f4b71Sopenharmony_ciexport default { 138e41f4b71Sopenharmony_ci data: { 139e41f4b71Sopenharmony_ci title: "", 140e41f4b71Sopenharmony_ci playState: "running" 141e41f4b71Sopenharmony_ci }, 142e41f4b71Sopenharmony_ci toggleState() { 143e41f4b71Sopenharmony_ci if (this.playState === "running") { 144e41f4b71Sopenharmony_ci this.playState = "paused"; 145e41f4b71Sopenharmony_ci } else { 146e41f4b71Sopenharmony_ci this.playState = "running"; 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci} 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```html 155e41f4b71Sopenharmony_ci<!-- xxx.hml --> 156e41f4b71Sopenharmony_ci<div id='img' class="img"></div> 157e41f4b71Sopenharmony_ci``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci```css 160e41f4b71Sopenharmony_ci/* xxx.css */ 161e41f4b71Sopenharmony_ci.img { 162e41f4b71Sopenharmony_ci width: 294px; 163e41f4b71Sopenharmony_ci height: 233px; 164e41f4b71Sopenharmony_ci background-image: url('common/heartBeat.jpg'); 165e41f4b71Sopenharmony_ci background-repeat: no-repeat; 166e41f4b71Sopenharmony_ci background-position: 0% 0%; 167e41f4b71Sopenharmony_ci background-size: 900%; 168e41f4b71Sopenharmony_ci animation-name: heartBeating; 169e41f4b71Sopenharmony_ci animation-duration: 1s; 170e41f4b71Sopenharmony_ci animation-delay: 0s; 171e41f4b71Sopenharmony_ci animation-fill-mode: forwards; 172e41f4b71Sopenharmony_ci animation-iteration-count: -1; 173e41f4b71Sopenharmony_ci animation-timing-function: steps(8, end); 174e41f4b71Sopenharmony_ci} 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci@keyframes heartBeating { 177e41f4b71Sopenharmony_ci from { background-position: 0% 0%;} 178e41f4b71Sopenharmony_ci to { background-position: 100% 0%;} 179e41f4b71Sopenharmony_ci} 180e41f4b71Sopenharmony_ci``` 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci```html 183e41f4b71Sopenharmony_ci<!-- xxx.hml --> 184e41f4b71Sopenharmony_ci<div class="container"> 185e41f4b71Sopenharmony_ci <div class="content"></div> 186e41f4b71Sopenharmony_ci</div> 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci```css 190e41f4b71Sopenharmony_ci/* xxx.css */ 191e41f4b71Sopenharmony_ci.container { 192e41f4b71Sopenharmony_ci flex-direction: column; 193e41f4b71Sopenharmony_ci justify-content: center; 194e41f4b71Sopenharmony_ci align-items: center; 195e41f4b71Sopenharmony_ci} 196e41f4b71Sopenharmony_ci.content { /* Component state 1 */ 197e41f4b71Sopenharmony_ci height: 200px; 198e41f4b71Sopenharmony_ci width: 200px; 199e41f4b71Sopenharmony_ci background-color: red; 200e41f4b71Sopenharmony_ci transition: all 5s ease 0s; 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci.content:active { /* Component state 2 */ 203e41f4b71Sopenharmony_ci height: 400px; 204e41f4b71Sopenharmony_ci width: 400px; 205e41f4b71Sopenharmony_ci background-color: blue; 206e41f4b71Sopenharmony_ci transition: all 5s linear 0s; 207e41f4b71Sopenharmony_ci} 208e41f4b71Sopenharmony_ci``` 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci> **NOTE** 213e41f4b71Sopenharmony_ci> 214e41f4b71Sopenharmony_ci> The @keyframes rule with **from** and **to** defined cannot be dynamically bound to an element. 215e41f4b71Sopenharmony_ci> 216e41f4b71Sopenharmony_ci> The following figure shows the meanings of **end** and **start** in the **steps** function. 217e41f4b71Sopenharmony_ci> 218e41f4b71Sopenharmony_ci> 