ts-transition-animation-shared-elements.md 3.0 KB
Newer Older
E
ester.zhou 已提交
1
# Shared Element Transition
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
A shared element transition is a transition animation applied to a component that is present on two pages. This component is called the shared element and can be set in the **sharedTransition** attribute.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
> **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

E
ester.zhou 已提交
10
## Attributes
Z
zengyawen 已提交
11 12


E
ester.zhou 已提交
13
| Name            | Parameter                                                        | Description                                                    |
E
ester.zhou 已提交
14
| ---------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
15
| sharedTransition | id: string,<br>{<br> duration?: number,<br> curve?: Curve \| string,<br> delay?: number,<br> motionPath?: <br>{<br> path: string,<br> form?: number,<br> to?: number,<br> rotatable?: boolean<br>},<br>zIndex?: number,<br>type?: [SharedTransitionEffectType](ts-appendix-enums.md#sharedtransitioneffecttype)<br>} | Transition of the shared element. If the same **id** value is configured for a component on the two pages, this component is considered as a shared element of the pages. If the **id** value is an empty string, no transition will be applied to the component.<br>- **id**: component ID.<br>- **duration**: animation duration.<br>Default value: **1000**<br>Unit: ms<br>Value range: [0, +∞)<br>The value **0** indicates that no animation is applied. A value less than 0 evaluates to the default value **1000**.<br>- **curve**: animation curve. The default curve is **Linear**. For details about the valid values, see [Curve](ts-animatorproperty.md).<br>- **delay**: animation delay.<br>Default value: **0**<br>Unit: ms<br>Value range: [0, +∞)<br>A value less than 0 evaluates to the value **0**.<br>- **motionPath**: motion path information. For details, see [Motion Path Animation](ts-motion-path-animation.md).<br>- **path**: path.<br>- **from**: start value.<br>- **to**: end value.<br>- **rotatable**: whether to rotate.<br>- **zIndex**: z-axis.<br>- **type**: animation type.|
Z
zengyawen 已提交
16 17 18


## Example
Z
zengyawen 已提交
19

E
ester.zhou 已提交
20
This example implements the custom transition of a shared image during redirection from one page to another, which is triggered by a click on the image.
E
ester.zhou 已提交
21 22 23

```ts
// xxx.ets
Z
zengyawen 已提交
24 25 26 27 28 29
@Entry
@Component
struct SharedTransitionExample {
  @State active: boolean = false

  build() {
E
ester.zhou 已提交
30 31 32 33 34 35 36 37
    Column() {
      Navigator({ target: 'pages/PageB', type: NavigationType.Push }) {
        Image($r('app.media.ic_health_heart')).width(50).height(50)
          .sharedTransition('sharedImage', { duration: 800, curve: Curve.Linear, delay: 100 })
      }.padding({ left: 20, top: 20 })
      .onClick(() => {
        this.active = true
      })
Z
zengyawen 已提交
38 39 40 41 42
    }
  }
}
```

E
ester.zhou 已提交
43 44
```ts
// PageB.ets
Z
zengyawen 已提交
45 46
@Entry
@Component
E
ester.zhou 已提交
47
struct pageBExample {
Z
zengyawen 已提交
48 49
  build() {
    Stack() {
E
ester.zhou 已提交
50 51
      Image($r('app.media.ic_health_heart')).width(150).height(150)
        .sharedTransition('sharedImage', { duration: 800, curve: Curve.Linear, delay: 100 })
E
ester.zhou 已提交
52
    }.width('100%').height('100%')
Z
zengyawen 已提交
53 54 55 56
  }
}
```

E
ester.zhou 已提交
57
![shared](figures/shared.gif)