ui-js-building-ui-animation.md 3.9 KB
Newer Older
G
ge-yafang 已提交
1
# Developing Animations
M
mamingshuai 已提交
2 3


G
ge-yafang 已提交
4 5 6 7
Animations are classified into [Static Animation](#static-animation) and [Continuous Animation](#continuous-animation).


## Static Animation
M
mamingshuai 已提交
8

Z
zengyawen 已提交
9
The 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:
M
mamingshuai 已提交
10

G
ge-yafang 已提交
11 12 13 14 15 16 17
- translate: Moves a specified component horizontally or vertically.

- scale: Scales a specified component horizontally or vertically to the required scale.

- rotate: Rotates a specified component by a specified angle along the horizontal axis, vertical axis, or center point.

For more information, see [Component Methods](../reference/arkui-js/js-components-common-methods.md). The following is an example:
M
mamingshuai 已提交
18 19 20 21 22 23 24 25 26 27 28


```
<!-- xxx.hml -->
<div class="container">
  <text class="translate">hello</text>
  <text class="rotate">hello</text>
  <text class="scale">hello</text>
</div>
```

G
ge-yafang 已提交
29

M
mamingshuai 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
```
/* xxx.css */
.container {
  flex-direction: column;
  align-items: center;
}
.translate {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform: translate(200px);
}
.rotate {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform-origin: 200px 100px;
  transform: rotateX(45deg);
}
.scale {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform: scaleX(1.5);
}
```


G
ge-yafang 已提交
61 62 63 64 65 66
  figure1 Static animation
  
  ![en-us_image_0000001223127724](figures/en-us_image_0000001223127724.png)


## Continuous Animation
M
mamingshuai 已提交
67 68 69 70 71

The static animation has only the start and end states. To set the transition state and conversion effect, use continuous animations.

The 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:

G
ge-yafang 已提交
72 73 74 75 76 77 78 79 80 81 82
- animation-name: Background color, opacity, width, height, and transformation type applied to the element after the animation is executed

- animation-delay and animation-duration: Element delay and duration after the animation is executed

- animation-timing-function: Speed curve of an animation, which makes the animation more fluent

- animation-iteration-count: Number of animation playback times

- animation-fill-mode: Whether to restore the initial state after the animation is executed

To 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:
M
mamingshuai 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98


```
<!-- xxx.hml -->
<div class="item-container">
  <text class="header">animation-name</text>
  <div class="item {{colorParam}}">
    <text class="txt">color</text>
  </div>
  <div class="item {{opacityParam}}">
    <text class="txt">opacity</text>
  </div>
  <input class="button" type="button" name="" value="show" onclick="showAnimation"/>
</div>
```

G
ge-yafang 已提交
99

M
mamingshuai 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
```
/* xxx.css */
.item-container {
  margin-right: 60px;
  margin-left: 60px;
  flex-direction: column;
}
.header {
  margin-bottom: 20px;
}
.item {
  background-color: #f76160;
}
.txt {
  text-align: center;
  width: 200px;
  height: 100px;
}
.button {
  width: 200px;
  font-size: 30px;
  background-color: #09ba07;
}
.color {
  animation-name: Color;
  animation-duration: 8000ms;
}
.opacity {
  animation-name: Opacity;
  animation-duration: 8000ms;
}
@keyframes Color {
  from {
    background-color: #f76160;
  }
  to {
    background-color: #09ba07;
  }
}
@keyframes Opacity {
  from {
    opacity: 0.9;
  }
  to {
    opacity: 0.1;
  }
}
```

G
ge-yafang 已提交
149

M
mamingshuai 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
```
// xxx.js
export default {
  data: {
    colorParam: '',
    opacityParam: '',
  },
  showAnimation: function () {
    this.colorParam = '';
    this.opacityParam = '';
    this.colorParam = 'color';
    this.opacityParam = 'opacity';
  },
}
```

G
ge-yafang 已提交
166 167 168
  figure2 Continuous animation effect
  
  ![en-us_image_0000001223287696](figures/en-us_image_0000001223287696.gif)