ts-page-transition-animation.md 10.4 KB
Newer Older
Z
zengyawen 已提交
1 2
# Page Transition

E
ester.zhou 已提交
3
The page transition navigates users between pages. You can customize page transitions by configuring the page entrance and exit components in the **pageTransition** API.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
> **NOTE**
>
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8
>
E
ester.zhou 已提交
9

Z
zengyawen 已提交
10

E
ester.zhou 已提交
11 12 13 14
| Name               | Parameter                                                        | Mandatory| Description                                                    |
| ------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| PageTransitionEnter | {<br>type?: RouteType,<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string,<br>delay?: number<br>} | No  | Page entrance animation.<br>- **type**: route type for the page transition effect to take effect.<br>Default value: **RouteType.None**<br>**NOTE**<br>If no match is found, the default page transition effect is used (which may vary according to the device). To disable the default page transition effect, set **duration** to **0**.<br>- **duration**: animation duration.<br>Unit: ms<br>- **curve**: animation curve. The value of the string type can be any of the following: "ease", "ease-in", "ease-out", "ease-in-out", "extreme-deceleration", "fast-out-linear-in", "fast-out-slow-in", "friction", "linear", "linear-out-slow-in", "rhythm", "sharp", "smooth".<br>Default value: **Curve.Linear**<br>- **delay**: animation delay.<br>Default value: **0**<br>Unit: ms|
| PageTransitionExit  | {<br>type?: RouteType,<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string,<br>delay?: number<br>} | No  | Page exit animation.<br>- **type**: route type for the page transition effect to take effect.<br>Default value: **RouteType.None**<br>**NOTE**<br>If no match is found, the default page transition effect is used (which may vary according to the device). To disable the default page transition effect, set **duration** to **0**.<br>- **duration**: animation duration, in milliseconds.<br>- **curve**: animation curve. The value range of the string type is the same as that of **PageTransitionEnter**.<br>Default value: **Curve.Linear**<br>- **delay**: animation delay.<br>Default value: **0**<br>Unit: ms|
Z
zengyawen 已提交
15

E
ester.zhou 已提交
16
## RouteType
Z
zengyawen 已提交
17

E
ester.zhou 已提交
18 19
| Name| Description                                                        |
| ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
20 21 22
| Pop  | Redirects to a specified page. To redirect the user from page B back to page A, set **RouteType** of **PageTransitionExit** to **None** or **Pop** for page B and set **RouteType** of **PageTransitionEnter** to **None** or **Pop** for page A.|
| Push | Redirects to the next page. To redirect the user from page A to page B, set **RouteType** of **PageTransitionExit** to **None** or **Push** for page A and set **RouteType** of **PageTransitionEnter** to **None** or **Push** for page B.|
| None | The page is not redirected. The animation specified by **PageTransitionEnter** takes effect for page entrance, and the animation specified by **PageTransitionExit** takes effect for page exit.|
Z
zengyawen 已提交
23 24 25 26


## Attributes

E
ester.zhou 已提交
27 28
| Name | Type                                                    | Mandatory| Description                                                    |
| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
29
| slide     | [SlideEffect](#slideeffect)                          | No  | Slide effect during page transition.<br>Default value: **SlideEffect.Right**|
E
ester.zhou 已提交
30
| translate | {<br>x? : number \| string,<br>y? : number \| string,<br>z? : number \| string<br>} | No  | Translation effect during page transition, which is the value of the start point of entrance and the end point of exit. When this parameter is set together with **slide**, the latter takes effect by default.<br>- **x**: translation distance along the x-axis.<br>- **y**: translation distance along the y-axis.<br>- **z**: translation distance along the y-axis.|
E
ester.zhou 已提交
31
| scale     | {<br>x? : number,<br>y? : number,<br>z? : number,<br>centerX? : number \| string,<br>centerY? : number \| string<br>} | No  | Scaling effect during page transition, which is the value of the start point of entrance and the end point of exit.<br>- **x**: scale ratio along the x-axis.<br>- **y**: scale ratio along the y-axis.<br>- **z**: scale ratio along the z-axis.<br>- **centerX** and **centerY**: scale center point.<br>- If the center point is 0, it refers to the upper left corner of the component. |
E
ester.zhou 已提交
32
| opacity   | number                                                       | No  | Opacity, which is the opacity value of the start point of entrance or the end point of exit.<br>Default value: **1**|
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34
## SlideEffect
Z
zengyawen 已提交
35

E
ester.zhou 已提交
36 37 38 39 40 41
| Name    | Description                       |
| ------ | ------------------------- |
| Left   | When set to Enter, slides in from the left. When set to Exit, slides out to the left.|
| Right  | When set to Enter, slides in from the right. When set to Exit, slides out to the right.|
| Top    | When set to Enter, slides in from the top. When set to Exit, slides out to the top.|
| Bottom | When set to Enter, slides in from the bottom. When set to Exit, slides out to the bottom.|
Z
zengyawen 已提交
42 43 44


## Events
Z
zengyawen 已提交
45

E
ester.zhou 已提交
46
| Name                                                         | Description                                                  |
E
ester.zhou 已提交
47
| ------------------------------------------------------------ | ------------------------------------------------------------ |
E
ester.zhou 已提交
48 49
| onEnter(event: (type?: RouteType, progress?: number) =&gt; void) | Invoked once every animation frame until the entrance animation ends, when the value of **progress** changes from 0 to 1. The input parameter is the normalized progress of the current entrance animation. The value range is 0–1.<br>- **type**: route type.<br>- **progress**: current progress. |
| onExit(event: (type?: RouteType, progress?: number) =&gt; void) | Invoked once every animation frame until the exit animation ends, when the value of **progress** changes from 0 to 1. The input parameter is the normalized progress of the current exit animation. The value range is 0–1.<br>- **type**: route type.<br>- **progress**: current progress. |
Z
zengyawen 已提交
50 51 52


## Example
Z
zengyawen 已提交
53 54 55

Customization method 1: The entrance animation of the current page is configured as fade-in, and the exit animation is configured as zoom-out.

E
ester.zhou 已提交
56
```ts
Z
zengyawen 已提交
57 58 59 60
// index.ets
@Entry
@Component
struct PageTransitionExample1 {
E
ester.zhou 已提交
61 62 63
  @State scale1: number = 1
  @State opacity1: number = 1

Z
zengyawen 已提交
64
  build() {
E
ester.zhou 已提交
65
    Column() {
Z
zengyawen 已提交
66
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
E
ester.zhou 已提交
67
        Image($r('app.media.bg1')).width('100%').height('100%')    // Store the image in the media folder.
Z
zengyawen 已提交
68
      }
E
ester.zhou 已提交
69
    }.scale({ x: this.scale1 }).opacity(this.opacity1)
Z
zengyawen 已提交
70
  }
E
ester.zhou 已提交
71
  // Customization method 1: Customize the transition process.
Z
zengyawen 已提交
72 73 74
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
E
ester.zhou 已提交
75 76
        this.scale1 = 1
        this.opacity1 = progress
Z
zengyawen 已提交
77 78 79
      }) // The onEnter callback is triggered frame by frame during the entrance process. The input parameter is the normalized progress of the animation (0% to 100%).
    PageTransitionExit({ duration: 1500, curve: Curve.Ease })
      .onExit((type: RouteType, progress: number) => {
E
ester.zhou 已提交
80 81
        this.scale1 = 1 - progress
        this.opacity1 = 1
Z
zengyawen 已提交
82 83 84 85 86
      }) // The onExit callback is triggered frame by frame during the exit process. The input parameter is the normalized progress of the animation (0% to 100%).
  }
}
```

E
ester.zhou 已提交
87
```ts
Z
zengyawen 已提交
88 89 90 91
// page1.ets
@Entry
@Component
struct AExample {
E
ester.zhou 已提交
92 93 94
  @State scale2: number = 1
  @State opacity2: number = 1

Z
zengyawen 已提交
95 96
  build() {
    Column() {
E
ester.zhou 已提交
97 98
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
        Image($r('app.media.bg2')).width('100%').height('100%')   // Store the image in the media folder.
Z
zengyawen 已提交
99
      }
E
ester.zhou 已提交
100
    }.width('100%').height('100%').scale({ x: this.scale2 }).opacity(this.opacity2)
Z
zengyawen 已提交
101
  }
E
ester.zhou 已提交
102
  // Customization method 1: Customize the transition process.
Z
zengyawen 已提交
103 104 105
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
E
ester.zhou 已提交
106 107
        this.scale2 = 1
        this.opacity2 = progress
Z
zengyawen 已提交
108 109 110
      }) // The onEnter callback is triggered frame by frame during the entrance process. The input parameter is the normalized progress of the animation (0% to 100%).
    PageTransitionExit({ duration: 1500, curve: Curve.Ease })
      .onExit((type: RouteType, progress: number) => {
E
ester.zhou 已提交
111 112 113
        this.scale2 = 1 - progress
        this.opacity2 = 1
      }) // The onExit callback is triggered frame by frame during the exit process. The input parameter is the normalized progress of the animation (0% to 100%).
Z
zengyawen 已提交
114 115 116 117
  }
}
```

Z
zengyawen 已提交
118
![en-us_image_0000001256978335](figures/en-us_image_0000001256978335.gif)
Z
zengyawen 已提交
119 120 121

Customization method 2: The entrance animation of the current page is configured to slide in from the left, and the exit animation is configured to zoom out with opacity change.

E
ester.zhou 已提交
122
```ts
Z
zengyawen 已提交
123 124 125 126
// index.ets 
@Entry
@Component
struct PageTransitionExample {
E
ester.zhou 已提交
127 128
  @State scale1: number = 1
  @State opacity1: number = 1
Z
zengyawen 已提交
129 130 131 132

  build() {
    Column() {
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
E
ester.zhou 已提交
133
        Image($r('app.media.bg1')).width('100%').height('100%')   // Store the image in the media folder.
Z
zengyawen 已提交
134
      }
E
ester.zhou 已提交
135
    }.scale({ x: this.scale1 }).opacity(this.opacity1)
Z
zengyawen 已提交
136 137
  }

E
ester.zhou 已提交
138
  // Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
139 140 141 142 143 144 145 146 147 148
  pageTransition() {
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
    PageTransitionExit({ delay: 100 })
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

E
ester.zhou 已提交
149
```ts
Z
zengyawen 已提交
150 151 152 153
// page1.ets
@Entry
@Component
struct PageTransitionExample1 {
E
ester.zhou 已提交
154 155
  @State scale2: number = 1
  @State opacity2: number = 1
E
ester.zhou 已提交
156

Z
zengyawen 已提交
157 158 159
  build() {
    Column() {
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
E
ester.zhou 已提交
160
        Image($r('app.media.bg2')).width('100%').height('100%')    // Store the image in the media folder.
Z
zengyawen 已提交
161
      }
E
ester.zhou 已提交
162
    }.scale({ x: this.scale2 }).opacity(this.opacity2)
Z
zengyawen 已提交
163 164
  }

E
ester.zhou 已提交
165
  // Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
166 167 168 169 170 171 172 173 174 175
  pageTransition() {
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
    PageTransitionExit({ delay: 100 })
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

Z
zengyawen 已提交
176
![en-us_image_0000001212058460](figures/en-us_image_0000001212058460.gif)