1# 转场样式
2
3>  **说明:**
4>  从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
5
6## 共享元素转场
7
8
9### 属性
10
11| 名称      | 类型     | 默认值  | 描述                                       |
12| ------- | ------ | ---- | ---------------------------------------- |
13| shareid | string | 无    | 进行共享元素转场时使用,若不配置,则转场样式不生效。共享元素转场当前支持的组件:list-item、image、text、button、label。 |
14
15
16### 样式
17
18| 名称                                | 类型     | 默认值      | 描述                                       |
19| --------------------------------- | ------ | -------- | ---------------------------------------- |
20| shared-transition-effect          | string | exchange | 配置共享元素转场时的入场样式。<br/>-&nbsp;exchange(默认值):源页面元素移动到目的页元素位置,并进行适当缩放。<br/>-&nbsp;static:目的页元素位置不变,用户可配置透明度动画。当前仅跳转目标页配置的static效果生效。 |
21| shared-transition-name            | string | -        | 转场时,目的页配置的样式优先生效。该样式用于配置共享元素的动画效果,一个由@keyframes定义的动画序列,支持transform和透明度动画。若共享元素效果与自定义的动画冲突,以自定义动画为准。 |
22| shared-transition-timing-function | string | friction | 转场时,目的页配置的样式优先生效。该属性定义了共享元素转场时的差值曲线。若不配置,默认使用friction曲线。 |
23
24
25### 注意事项
26
271. 若同时配置了共享元素转场和自定义页面转场样式,页面转场效果以自定义效果为准。
28
292. 共享元素的exchange效果类似下图。
30
31**图1** 共享元素转场默认效果
32![zh-cn_image_0000001238424309](figures/zh-cn_image_0000001238424309.png)
33
343. 共享元素动画对元素的边框、背景色不生效。
35
364. 共享元素转场时,由于页面元素会被隐藏,故页面元素配置的动画样式/动画方法失效。
37
385. 动态修改shareid<sup>5+</sup>:组件A的shareid被组件B的shareid覆盖,则组件A的共享元素效果失效,即使组件B的shareid被修改,此时组件A的共享元素效果也不会恢复。
39
40
41### 示例
42
43PageA跳转到PageB,跳转的共享元素为image, shareid为“shareImage”。
44
45```html
46<!-- PageA -->
47<!-- xxx.hml -->
48<div>
49  <list>
50    <list-item type="description">
51      <image src="item.jpg" shareid="shareImage" onclick="jump" class="shared-transition-style"></image>
52    </list-item>
53    <list-item>
54      <text onclick="jump">Click on picture to Jump to ths details</text>
55    </list-item>
56  </list>
57</div>
58```
59
60```js
61// xxx.js
62import router from '@ohos.router';
63export default {
64  jump() {
65    router.push({
66      // 路径要与config.json配置里面的相同
67      url: 'pages/detailpage',
68    });
69  },
70}
71```
72
73```css
74/* xxx.css */
75.shared-transition-style {
76  shared-transition-effect: exchange;
77  shared-transition-name: shared-transition;
78}
79@keyframes shared-transition {
80  from { opacity: 0; }
81  to { opacity: 1; }
82}
83```
84
85```html
86<!-- PageB -->
87<!-- xxx.hml -->
88<div>
89  <image src="itemDetail.jpg" shareid="shareImage" onclick="jumpBack" class="shared-transition-style"></image>
90</div>
91```
92
93```js
94// xxx.js
95import router from '@ohos.router';
96export default {
97  jumpBack() {
98    router.back();
99  },
100}
101```
102
103```css
104/* xxx.css */
105.shared-transition-style {
106  shared-transition-effect: exchange;
107  shared-transition-name: shared-transition;
108}
109@keyframes shared-transition {
110  from { opacity: 0; }
111  to { opacity: 1; }
112}
113```
114
115
116## 卡片转场样式
117
118>  **说明:**
119>  卡片转场无法和其他转场(包括共享元素转场和自定义转场)共同使用。
120
121
122### 样式
123
124| 名称                | 类型     | 默认值  | 描述                                       |
125| ----------------- | ------ | ---- | ---------------------------------------- |
126| transition-effect | string | -    | 用于配置当前页面中的某个组件在卡片转场过程中是否进行转场动效,当前支持如下配置:<br/>-&nbsp;unfold:配置这个属性的组件,如在卡片的上方,则向上移动一个卡片的高度,如在卡片的下方,则向下移动一个卡片的高度。<br/>-&nbsp;none:转场过程中没有动效。 |
127
128
129### 示例
130
131source_page包含顶部内容以及卡片列表,点击卡片可以跳转到target_page。
132
133```html
134<!-- source_page -->
135<!-- xxx.hml -->
136<div class="container">
137  <div class="outer">
138    <text style="font-size: 23px; margin-bottom: 20px" >MAIN TITLE</text>
139  </div>
140  <list style="width:340px;height:600px;flex-direction:column;justify-content:center;align-items:center">
141    <list-item type="listItem" class="item" card="true" for="list" id="{{$item.id}}" onclick="jumpPage({{$item.id}}, {{$item.url}})">
142      <text style="margin-left: 10px; font-size: 23px;">{{$item.title}}</text>
143    </list-item>
144  </list>
145</div>
146```
147
148```js
149// xxx.js
150import router from '@ohos.router'
151export default {
152  data: { list: [] },
153  onInit() {
154    for(var i = 0; i < 10; i++) {
155      var item = { url: "pages/card_transition/target_page/index",
156                   title: "this is title" + i, id: "item_" + i }
157      this.list.push(item);
158    }
159  },
160  jumpPage(id, url) {
161    var cardId = this.$element(id).ref;
162    router.push({ url: url, params : { ref : cardId } });
163  }
164}
165```
166
167```css
168/* xxx.css */
169.container {
170  width: 100%;
171  height: 100%;
172  flex-direction: column;
173  align-items: center;
174  background-color: #ABDAFF;
175}
176.item {
177  height: 80px;
178  background-color: #FAFAFA;
179  margin-top: 2px;
180}
181.outer {
182  width: 300px;
183  height: 100px;
184  align-items: flex-end;
185  transition-effect: unfold;
186}
187```
188
189```html
190<!-- target_page -->
191<!-- xxx.hml -->
192<div class="container">
193  <div class="div">
194    <text style="font-size: 30px">this is detail</text>
195  </div>
196</div>
197```
198
199```css
200/* xxx.css */
201.container {
202  width: 100%;
203  height: 100%;
204  flex-direction: column;
205  align-items: center;
206  background-color: #EBFFD7;
207}
208.div {
209  height: 600px;
210  flex-direction: column;
211  align-items: center;
212  justify-content: center;
213}
214```
215
216![zh-cn_image_0000001193544358](figures/zh-cn_image_0000001193544358.gif)
217
218
219## 页面转场样式
220
221
222### 样式
223
224| 名称                         | 类型     | 默认值           | 描述                                       |
225| -------------------------- | ------ | ------------- | ---------------------------------------- |
226| transition-enter           | string | -             | 与@keyframes配套使用,支持transform和透明度动画,详见[动画样式 表 @keyframes属性说明](js-components-common-animation.md)。 |
227| transition-exit            | string | -             | 与\@keyframes配套使用,支持transform和透明度动画,详见[动画样式 表 @keyframes属性说明](js-components-common-animation.md)。 |
228| transition-duration        | string | 跟随设备默认的页面转场时间 | 支持的单位为[s(秒)\|ms(毫秒)&nbsp;],默认单位为ms,未配置时使用系统默认值。 |
229| transition-timing-function | string | friction      | 描述转场动画执行的速度曲线,用于使转场更为平滑。详细参数见[动画样式](js-components-common-animation.md)中“animation-timing-function”有效值说明。 |
230
231
232### 注意事项
233
2341. 配置自定义转场时,建议配置页面背景色为不透明颜色,否则在转场过程中可能会出现衔接不自然的现象。
235
2362. transition-enter和transition-exit可单独配置,没有配置时使用系统默认的参数。
237
2383. transition-enter/transition-exit说明如下:
239
240   a. push场景下:进入页面栈的Page2.js应用transition-enter描述的动画配置;进入页面栈第二位置的Page1.js应用transition-exit描述的动画配置。
241   ![zh-cn_image_0000001193704354](figures/zh-cn_image_0000001193704354.png)
242
243   b. back场景下:退出页面栈的Page2.js应用transition-enter描述的动画配置,并进行倒播;从页面栈第二位置进入栈顶位置的Page1.js应用transition-exit描述的动画配置,并进行倒播。
244   ![zh-cn_image_0000001238184345](figures/zh-cn_image_0000001238184345.png)
245
246### 示例
247
248Page1有一个不透明盒子,点击盒子会跳转到Page2,当点击Page2中的盒子,会回退到Page1页面。
249
2501. Page1
251
252   ```html
253   <!-- xxx.hml -->
254   <div class="container">
255       <text>index</text>
256       <div class="move_page" onclick="jump"></div>
257   </div>
258   ```
259
260   ```js
261   // xxx.js
262   import router from '@ohos.router';
263   export default {
264       data: {
265
266       },
267       jump() {
268           router.push({
269               url:'pages/transition2/transition2'
270           })
271       }
272   }
273   ```
274
275   ```css
276   /* xxx.css */
277   .container {
278       flex-direction: column;
279       justify-content: center;
280       align-items: center;
281       width: 100%;
282       height: 100%;
283   }
284   .move_page {
285       width: 100px;
286       height: 100px;
287       background-color: #72d3fa;
288       transition-enter: go_page;
289       transition-exit: exit_page;
290       transition-duration: 5s;
291       transition-timing-function: friction;
292   }
293
294   @keyframes go_page {
295       from {
296           opacity: 0;
297           transform: translate(0px) rotate(60deg) scale(1.0);
298       }
299
300       to {
301           opacity: 1;
302           transform: translate(100px) rotate(360deg) scale(1.0);
303       }
304   }
305   @keyframes exit_page {
306       from {
307           opacity: 1;
308           transform: translate(200px) rotate(60deg) scale(2);
309       }
310
311       to {
312           opacity: 0;
313           transform: translate(200px) rotate(360deg) scale(2);
314       }
315   }
316   ```
317
318
3192. Page2
320
321   ```html
322   <!-- xxx.hml -->
323   <div class="container">
324       <text>transition</text>
325       <div class="move_page" onclick="jumpBack"></div>
326   </div>
327   ```
328
329   ```js
330   // xxx.js
331   import router from '@ohos.router';
332   export default {
333       data: {
334
335       },
336       jumpBack() {
337           router.back()
338       }
339   }
340   ```
341
342   ```css
343   /* xxx.css */
344   .container {
345       flex-direction: column;
346       justify-content: center;
347       align-items: center;
348       width: 100%;
349       height: 100%;
350   }
351
352   .move_page {
353       width: 100px;
354       height: 100px;
355       background-color: #f172fa;
356       transition-enter: go_page;
357       transition-exit: exit_page;
358       transition-duration: 5s;
359       transition-timing-function: ease;
360   }
361
362   @keyframes go_page {
363       from {
364           opacity: 0;
365           transform:translate(100px) rotate(0deg) scale(1.0);
366       }
367       to {
368           opacity: 1;
369           transform:translate(100px) rotate(180deg) scale(2.0);
370       }
371   }
372
373   @keyframes exit_page {
374       from {
375           opacity: 1;
376           transform: translate(0px) rotate(60deg) scale(1);
377       }
378       to {
379           opacity: 0;
380           transform: translate(0px) rotate(360deg) scale(1);
381       }
382   }
383   ```
384
385   ![transition](figures/transition.gif)
386