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