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


4
> **NOTE**<br>
Z
zengyawen 已提交
5 6 7 8 9
> This animation is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


Customize the page transition animations by configuring the page entrance and exit components in the global **pageTransition** method.

10
## APIs
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
| Name | Parameter | Description |
Z
zengyawen 已提交
13
| -------- | -------- | -------- |
E
ester.zhou 已提交
14 15
| PageTransitionEnter | Object | Page entrance component, which is used to customize the entrance effect of the current page. For details, see animation parameters. |
| PageTransitionExit | Object | Page exit component, which is used to customize the exit effect of the current page. For details, see animation parameters. |
Z
zengyawen 已提交
16 17 18


- Animation parameters
19
  | Name | Type | Default Value | Mandatory | Description |
Z
zengyawen 已提交
20
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
21 22 23 24
  | type | RouteType | - | No | If this parameter is not set, the reverse playback effect as pop switches to push is used. |
  | duration | number | 1000 | No | Animation duration, in ms. |
  | curve | Curve \| Curves | Linear | No | Animation curve. For details about the valid values, see **Curve enums**. |
  | delay | number | 0 | No | Animation delay, in ms. Delayed animation is disabled by default. |
Z
zengyawen 已提交
25 26 27


- RouteType enums
28
  | Name | Description |
Z
zengyawen 已提交
29
  | -------- | -------- |
E
ester.zhou 已提交
30 31
  | Pop | When page A jumps to page B, page A is Exit+Push, and page B is Enter+Push. |
  | Push | When page B returns to page A, page A is Enter+Pop, and page B is Exit+Pop. |
Z
zengyawen 已提交
32 33 34 35 36 37


## Attributes

The **PageTransitionEnter** and **PageTransitionExit** components support the following attributes:

E
ester.zhou 已提交
38
| Name | Type | Default Value | Mandatory | Description |
Z
zengyawen 已提交
39
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
40 41 42 43
| slide | SlideEffect | SlideEffect.Right | No | Slide effect during page transition. For details about the valid values, see **SlideEffect enums**. |
| translate | {<br/>x? : number,<br/>y? : number,<br/>z? : number<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. |
| scale | {<br/>x? : number,<br/>y? : number,<br/>z? : number,<br/>centerX? : number,<br/>centerY? : number<br/>} | - | No | Scaling effect during page transition, which is the value of the start point of entrance and the end point of exit. |
| opacity | number | 1 | No | Opacity, which is the opacity value of the start point of entrance or the end point of exit. |
Z
zengyawen 已提交
44 45

- SlideEffect enums
46
  | Name | Description |
Z
zengyawen 已提交
47
  | -------- | -------- |
E
ester.zhou 已提交
48 49 50 51
  | 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 已提交
52 53 54


## Events
Z
zengyawen 已提交
55 56 57

The PageTransitionEnter and PageTransitionExit components support the following events:

E
ester.zhou 已提交
58
| Event | Description |
Z
zengyawen 已提交
59
| -------- | -------- |
60 61
| onEnter(type: RouteType, progress: number) =&gt; void | Callback invoked when page entrance occurs. The input parameter is the normalized progress of the current entrance animation. The value range is 0–1. |
| onExit(type: RouteType, progress: number) =&gt; void | Callback invoked when page exit occurs. The input parameter is the normalized progress of the current exit animation. The value range is 0–1. |
Z
zengyawen 已提交
62 63 64


## Example
Z
zengyawen 已提交
65 66 67

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

Z
zengyawen 已提交
68

Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
```
// index.ets
@Entry
@Component
struct PageTransitionExample1 {
  @State scale: number = 1
  @State opacity: number = 1
  @State active: boolean = false
  build() {
  Column() {
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
        Image($r('app.media.bg1')).width("100%").height("100%")
      }
      .onClick(() => {
        this.active = true
      })
    }.scale({ x: this.scale }).opacity(this.opacity)
  }
// Customization method 1: Customize the transition process.
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
        this.scale = 1
        this.opacity = progress
      }) // 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) => {
        this.scale = 1 - progress
        this.opacity = 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 已提交
103

Z
zengyawen 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
```
// page1.ets
@Entry
@Component
struct AExample {
  @State scale: number = 1
  @State opacity: number = 1
  @State active: boolean = false
  build() {
    Column() {
      Navigator({ target: 'pages/index' ,type: NavigationType.Push}) {
        Image($r('app.media.bg2')).width("100%").height("100%")
      }
    }.height("100%").width("100%").scale({ x: this.scale }).opacity(this.opacity)
  }
// Customization method 1: Customize the transition process.
  pageTransition() {
    PageTransitionEnter({ duration: 1200, curve: Curve.Linear })
      .onEnter((type: RouteType, progress: number) => {
        this.scale = 1
        this.opacity = progress
      }) // 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) => {
        this.scale = 1 - progress
        this.opacity = 1
      }) // The onExit callback is triggered frame by frame during the exi process. The input parameter is the normalized progress of the animation (0% to 100%).
  }
}
```

Z
zengyawen 已提交
135
![en-us_image_0000001256978335](figures/en-us_image_0000001256978335.gif)
Z
zengyawen 已提交
136 137 138

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.

Z
zengyawen 已提交
139

Z
zengyawen 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
```
// index.ets 
@Entry
@Component
struct PageTransitionExample {
  @State scale: number = 1
  @State opacity: number = 1
  @State active: boolean = false

  build() {
    Column() {
      Navigator({ target: 'pages/page1', type: NavigationType.Push }) {
        Image($r('app.media.bg1')).width("100%").height("100%")
      }
      .onClick(() => {
        this.active = true
      })
    }.scale({ x: this.scale }).opacity(this.opacity)
  }

// Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity.
  pageTransition() {
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
    PageTransitionExit({ delay: 100 })
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

Z
zengyawen 已提交
171

Z
zengyawen 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
```
// page1.ets
@Entry
@Component
struct PageTransitionExample1 {
  @State scale: number = 1
  @State opacity: number = 1
  @State active: boolean = false

  build() {
    Column() {
      Navigator({ target: 'pages/index', type: NavigationType.Push }) {
        Image($r('app.media.bg2')).width  ("100%").height("100%")
      }
      .onClick(() => {
        this.active = true
      })
    }.scale({ x: this.scale }).opacity(this.opacity)
  }

// Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity.
  pageTransition() {
    PageTransitionEnter({ duration: 1200 })
      .slide(SlideEffect.Left)
    PageTransitionExit({ delay: 100 })
      .translate({ x: 100.0, y: 100.0 })
      .opacity(0)
  }
}
```

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