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

E
ester.zhou 已提交
3
You can customize the page entrance and exit animations in the **pageTransition** API for transition between pages.
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
| Name               | Parameter                                                        | Mandatory| Description                                                    |
| ------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
13 14
| PageTransitionEnter | {<br>type?: RouteType,<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string \| [ICurve](../apis/js-apis-curve.md#icurve)<sup>10+</sup>,<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>- **duration**: animation duration.<br>Unit: ms<br>Default value: **1000**<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>Unit: ms<br>Default value: **0**<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**.|
| PageTransitionExit  | {<br>type?: RouteType,<br>duration?: number,<br>curve?: [Curve](ts-appendix-enums.md#curve) \| string \| [ICurve](../apis/js-apis-curve.md#icurve)<sup>10+</sup>,<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>- **duration**: animation duration.<br>Unit: ms<br>Default value: **1000**<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>Unit: ms<br>Default value: **0**<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**.|
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.|
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 32
| 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. The default values are both **"50%"**, indicating that the center point of the page.<br>- If the center point is (0, 0), it refers to the upper left corner of the component.<br>|
| opacity   | number                                                       | No  | Opacity, which is the opacity value of the start point of entrance or the end point of exit.|
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.<br> <br> |
| 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.<br> <br> |
Z
zengyawen 已提交
50 51 52


## Example
Z
zengyawen 已提交
53

E
ester.zhou 已提交
54
Example 1: Apply the entrance animation of fade-in and the exit animation of zoom-out.
Z
zengyawen 已提交
55

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
  // 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
      }) // 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%).
E
ester.zhou 已提交
78
    PageTransitionExit({ duration: 1200, curve: Curve.Ease })
Z
zengyawen 已提交
79
      .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
  // 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
      }) // 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%).
E
ester.zhou 已提交
109
    PageTransitionExit({ duration: 1200, curve: Curve.Ease })
Z
zengyawen 已提交
110
      .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
  }
}
```

E
ester.zhou 已提交
118
![pageTransition1](figures/pageTransition1.gif)
Z
zengyawen 已提交
119

E
ester.zhou 已提交
120
Example 2: Apply the entrance animation of sliding in from the left and the exit animation of translating with opacity change.
Z
zengyawen 已提交
121

E
ester.zhou 已提交
122
```ts
Z
zengyawen 已提交
123 124 125 126 127 128 129
// index.ets 
@Entry
@Component
struct PageTransitionExample {
  build() {
    Column() {
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
E
ester.zhou 已提交
130
        Image($r('app.media.bg1')).width('100%').height('100%')   // Store the image in the media folder.
Z
zengyawen 已提交
131
      }
E
ester.zhou 已提交
132
    }
Z
zengyawen 已提交
133 134
  }

E
ester.zhou 已提交
135
  // Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
136
  pageTransition() {
E
ester.zhou 已提交
137
    // Set the duration of the entrance animation to 1200 ms, in the purpose of matching the duration of the exit animation of the other page.
Z
zengyawen 已提交
138 139
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
E
ester.zhou 已提交
140 141
    // Set the duration of the exit animation to 1000 ms, in the purpose of matching the duration of the entrance animation of the other page.
    PageTransitionExit({ duration: 1000 })
Z
zengyawen 已提交
142 143 144 145 146 147
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

E
ester.zhou 已提交
148
```ts
Z
zengyawen 已提交
149 150 151 152 153 154 155
// page1.ets
@Entry
@Component
struct PageTransitionExample1 {
  build() {
    Column() {
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
E
ester.zhou 已提交
156
        Image($r('app.media.bg2')).width('100%').height('100%')    // Store the image in the media folder.
Z
zengyawen 已提交
157
      }
E
ester.zhou 已提交
158
    }
Z
zengyawen 已提交
159 160
  }

E
ester.zhou 已提交
161
  // Use the default effects provided by the system, such as translation, scaling, and opacity.
Z
zengyawen 已提交
162
  pageTransition() {
E
ester.zhou 已提交
163 164
    // Set the duration of the entrance animation to 1000 ms, in the purpose of matching the duration of the exit animation of the other page.
    PageTransitionEnter({ duration: 1000 })
Z
zengyawen 已提交
165
      .slide(SlideEffect.Left)
E
ester.zhou 已提交
166 167
    // Set the duration of the exit animation to 1200 ms, in the purpose of matching the duration of the entrance animation of the other page.
    PageTransitionExit({ duration: 1200 })
Z
zengyawen 已提交
168 169 170 171 172 173
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

E
ester.zhou 已提交
174
![pageTransition2](figures/pageTransition2.gif)