1# Page Transition (pageTransition) 2 3When performing route switching using the [router](../js-apis-router.md), you can customize entrance and exit transition animations between pages by implementing the **pageTransition** API. For details, see [Page Transition Animation](../../../ui/arkts-page-transition-animation.md). 4 5> **NOTE** 6> 7> This feature is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8> 9> To achieve a better transition effect, you are advised to use the [Navigation](../../../ui/arkts-navigation-navigation.md) component and [modal transition](../../../ui/arkts-modal-transition.md). 10 11## pageTransition 12 13pageTransition?(): void 14 15Defines the transition animation to play when the user accesses this page or is redirected from this page to another page. 16 17**Atomic service API**: This API can be used in atomic services since API version 11. 18 19**System capability**: SystemCapability.ArkUI.ArkUI.Full 20 21## PageTransitionEnter 22 23PageTransitionEnter(value: PageTransitionOptions) 24 25Sets the page entrance animation. Inherits from [CommonTransition](#commontransition). 26 27**Atomic service API**: This API can be used in atomic services since API version 11. 28 29**System capability**: SystemCapability.ArkUI.ArkUI.Full 30 31**Parameters** 32 33| Name| Type | Mandatory| Description | 34| ------ | ------------------------------------------------------ | ---- | -------------------- | 35| value | [PageTransitionOptions](#pagetransitionoptions) | Yes | Page entrance animation.| 36 37### onEnter 38 39onEnter(event: PageTransitionCallback): PageTransitionEnterInterface 40 41Invoked on a per-frame basis until the entrance animation is complete, with the **progress** parameter changing from 0 to 1. 42 43**Atomic service API**: This API can be used in atomic services since API version 11. 44 45**System capability**: SystemCapability.ArkUI.ArkUI.Full 46 47**Parameters** 48 49| Name| Type | Mandatory| Description | 50| ------ | ----------------------------------------------------------------- | ---- | ------------------------------------------------ | 51| event | [PageTransitionCallback](#pagetransitioncallback18) | Yes | Callback invoked on a per-frame basis until the entrance animation is complete, with the **progress** parameter changing from 0 to 1.| 52 53**Example** 54 55```js 56 pageTransition() { 57 PageTransitionEnter({ duration: 1200, curve: Curve.Linear }) 58 // During the transition animation, the entrance animation has a type that represents the route type, and a progress that increases from 0 to 1. 59 .onEnter((type: RouteType, progress: number) => { 60 // Service logic 61 }) 62 } 63``` 64 65## PageTransitionExit 66 67PageTransitionExit(value: PageTransitionOptions) 68 69Sets the page exit animation. Inherits from [CommonTransition](#commontransition). 70 71**Atomic service API**: This API can be used in atomic services since API version 11. 72 73**System capability**: SystemCapability.ArkUI.ArkUI.Full 74 75**Parameters** 76 77| Name| Type | Mandatory| Description | 78| ------ | ------------------------------------------------------- | ---- | -------------------- | 79| value | [PageTransitionOptions](#pagetransitionoptions) | Yes | Page exit animation.| 80 81### onExit 82 83onExit(event: PageTransitionCallback): PageTransitionExitInterface 84 85Invoked on a per-frame basis until the exit animation is complete, with the **progress** parameter changing from 0 to 1. 86 87**System capability**: SystemCapability.ArkUI.ArkUI.Full 88 89**Atomic service API**: This API can be used in atomic services since API version 11. 90 91**Parameters** 92 93| Name| Type | Mandatory| Description | 94| ------ | ----------------------------------------------------------------- | ---- | ------------------------------------------------ | 95| event | [PageTransitionCallback](#pagetransitioncallback18) | Yes | Callback invoked on a per-frame basis until the exit animation is complete, with the **progress** parameter changing from 0 to 1.| 96 97**Example** 98 99```js 100 pageTransition() { 101 PageTransitionExit({ duration: 1200, curve: Curve.Linear }) 102 // During the transition animation, the exit animation has a type that represents the route type, and a progress that increases from 0 to 1. 103 .onExit((type: RouteType, progress: number) => { 104 // Service logic 105 }) 106 } 107``` 108 109## PageTransitionOptions 110 111**Atomic service API**: This API can be used in atomic services since API version 11. 112 113**System capability**: SystemCapability.ArkUI.ArkUI.Full 114 115| Name | Type | Mandatory| Description | 116| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 117| type | [RouteType](#routetype) | No | Route type for the page transition effect to take effect.<br>Default value: **RouteType.None** | 118| duration | number | No | Animation duration.<br>Unit: ms<br>Default value: **1000**<br>Value range: [0, +∞) | 119| curve | [Curve](ts-appendix-enums.md#curve) \| string \| [ICurve](../js-apis-curve.md#icurve9)<sup>10+</sup> | No | Animation curve.<br>You are advised to specify the curve using the **Curve** or **ICurve** type.<br>For the string type, this parameter indicates an animation interpolation curve. For available values, see the **curve** parameter in [AnimateParam](./ts-explicit-animation.md#animateparam).<br>Default value: **Curve.Linear**| 120| delay | number | No | 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**.| 121 122## CommonTransition 123 124Defines a common transition animation for page transitions. 125 126**Atomic service API**: This API can be used in atomic services since API version 11. 127 128**System capability**: SystemCapability.ArkUI.ArkUI.Full 129 130### constructor 131 132constructor() 133 134A constructor used to create a common transition animation. 135 136**Atomic service API**: This API can be used in atomic services since API version 11. 137 138**System capability**: SystemCapability.ArkUI.ArkUI.Full 139 140### slide 141 142slide(value: SlideEffect): T 143 144Sets the slide-in and slide-out effects for page transitions. 145 146**Atomic service API**: This API can be used in atomic services since API version 11. 147 148**System capability**: SystemCapability.ArkUI.ArkUI.Full 149 150**Parameters** 151 152| Name | Type | Mandatory| Description | 153| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 154| value | [SlideEffect](#slideeffect) | Yes | Slide-in and slide-out effects for page transitions.| 155 156### translate 157 158translate(value: TranslateOptions): T 159 160Sets the translation effect for page transitions. 161 162**Atomic service API**: This API can be used in atomic services since API version 11. 163 164**System capability**: SystemCapability.ArkUI.ArkUI.Full 165 166**Parameters** 167 168| Name| Type | Mandatory| Description | 169| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 170| value | [TranslateOptions](ts-universal-attributes-transformation.md#translateoptions) | Yes | Translation effect for page transitions, specifying the start value for entrance and the end value for 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.| 171 172### scale 173 174scale(value: ScaleOptions): T 175 176Sets the scaling effect for page transitions. 177 178**Atomic service API**: This API can be used in atomic services since API version 11. 179 180**System capability**: SystemCapability.ArkUI.ArkUI.Full 181 182**Parameters** 183 184| Name| Type | Mandatory| Description | 185| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 186| value | [ScaleOptions](ts-universal-attributes-transformation.md#scaleoptions) | Yes | Scaling effect for page transitions, specifying the start value for entrance and the end value for exit.<br>- **x**: scale factor along the x-axis.<br>- **y**: scale factor along the y-axis.<br>- **z**: scale factor along the z-axis.<br>- **centerX** and **centerY**: scaling center. The default values are both **"50%"**, meaning the center of the page is used as the scaling center by default.<br>- If the center point is (0, 0), it refers to the upper left corner of the component.| 187 188### opacity 189 190opacity(value: number): T 191 192Sets the starting opacity value for entrance or the ending opacity value for exit. 193 194**Atomic service API**: This API can be used in atomic services since API version 11. 195 196**System capability**: SystemCapability.ArkUI.ArkUI.Full 197 198**Parameters** 199 200| Name | Type | Mandatory| Description | 201| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 202| value | number | Yes | Starting opacity value for entrance or the ending opacity value for exit.<br>Value range: [0, 1]| 203 204## PageTransitionCallback<sup>18+</sup> 205 206type PageTransitionCallback = (type: RouteType, progress: number) => void 207 208Represents the callback for page transition events. 209 210**Atomic service API**: This API can be used in atomic services since API version 18. 211 212**System capability**: SystemCapability.ArkUI.ArkUI.Full 213 214| Name | Type | Mandatory| Description | 215| ------ | ------ | ---- | ---------------- | 216| type | [RouteType](#routetype) | Yes| Type of page transition.| 217| progress | number | Yes| Transition progress, ranging from 0 to 1.| 218 219 ## RouteType 220 221**System capability**: SystemCapability.ArkUI.ArkUI.Full 222 223**Atomic service API**: This API can be used in atomic services since API version 11. 224 225 226| Name| Description | 227| ---- | ------------------------------------------------------------ | 228| 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.| 229| 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.| 230| 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.| 231 232## SlideEffect 233 234**Atomic service API**: This API can be used in atomic services since API version 11. 235 236**System capability**: SystemCapability.ArkUI.ArkUI.Full 237 238| Name | Description | 239| ------------------- | ------------------------------------------------------------ | 240| Left | When set to Enter, slides in from the left. When set to Exit, slides out to the left.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 241| Right | When set to Enter, slides in from the right. When set to Exit, slides out to the right.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 242| Top | When set to Enter, slides in from the top. When set to Exit, slides out to the top.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 243| Bottom | When set to Enter, slides in from the bottom. When set to Exit, slides out to the bottom.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 244| START<sup>12+</sup> | Left-to-right scripts: When set to Enter, slides in from the left; when set to Exit, slides out to the left. Right-to-left scripts: When set to Enter, slides in from the right; when set to Exit, slides out to the right.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 245| END<sup>12+</sup> | Left-to-right scripts: When set to Enter, slides in from the right; when set to Exit, slides out to the right. Right-to-left scripts: When set to Enter, slides in from the left; when set to Exit, slides out to the left.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 246 247## Example 248 249### Example 1: Configuring Entrance and Exit Animations 250 251Method 1: Configure different entrance and exit animations based on different transition types. 252 253```ts 254// Index.ets 255@Entry 256@Component 257struct Index { 258 @State scale1: number = 1; 259 @State opacity1: number = 1; 260 261 build() { 262 Column() { 263 Image($r("app.media.transition_image1")).width('100%').height('100%') 264 } 265 .width('100%') 266 .height('100%') 267 .scale({ x: this.scale1 }) 268 .opacity(this.opacity1) 269 .onClick(() => { 270 this.getUIContext().getRouter().pushUrl({ url: 'pages/Page1' }); 271 }) 272 } 273 274 pageTransition() { 275 PageTransitionEnter({ duration: 1200, curve: Curve.Linear }) 276 .onEnter((type: RouteType, progress: number) => { 277 if (type == RouteType.Push || type == RouteType.Pop) { 278 this.scale1 = progress; 279 this.opacity1 = progress; 280 } 281 }) 282 PageTransitionExit({ duration: 1200, curve: Curve.Ease }) 283 .onExit((type: RouteType, progress: number) => { 284 if (type == RouteType.Push) { 285 this.scale1 = 1 - progress; 286 this.opacity1 = 1 - progress; 287 } 288 }) 289 } 290} 291``` 292 293```ts 294// Page1.ets 295@Entry 296@Component 297struct Page1 { 298 @State scale2: number = 1; 299 @State opacity2: number = 1; 300 301 build() { 302 Column() { 303 Image($r("app.media.transition_image2")).width('100%').height('100%') // Store the image in the media folder. 304 } 305 .width('100%') 306 .height('100%') 307 .scale({ x: this.scale2 }) 308 .opacity(this.opacity2) 309 .onClick(() => { 310 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 311 }) 312 } 313 314 pageTransition() { 315 PageTransitionEnter({ duration: 1200, curve: Curve.Linear }) 316 .onEnter((type: RouteType, progress: number) => { 317 if (type == RouteType.Push || type == RouteType.Pop) { 318 this.scale2 = progress; 319 } 320 this.opacity2 = progress; 321 }) 322 PageTransitionExit({ duration: 1200, curve: Curve.Ease }) 323 .onExit((type: RouteType, progress: number) => { 324 if (type == RouteType.Pop) { 325 this.scale2 = 1 - progress; 326 this.opacity2 = 1 - progress; 327 } 328 }) 329 } 330} 331``` 332 333 334 335Method 2: Configure the entrance animation of sliding in from the left and the exit animation of translating with opacity change. 336 337```ts 338// Index.ets 339@Entry 340@Component 341struct Index { 342 build() { 343 Column() { 344 Image($r('app.media.bg1')).width('100%').height('100%') // The image is stored in the media folder. 345 } 346 .onClick(() => { 347 this.getUIContext().getRouter().pushUrl({ url: 'pages/Page1' }); 348 }) 349 } 350 351 // Use the default effects provided by the system, such as translation, scaling, and opacity. 352 pageTransition() { 353 // 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. 354 PageTransitionEnter({ duration: 1200 }) 355 .slide(SlideEffect.Left) 356 // 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. 357 PageTransitionExit({ duration: 1000 }) 358 .translate({ x: 100.0, y: 100.0 }) 359 .opacity(0) 360 } 361} 362``` 363 364```ts 365// Page1.ets 366@Entry 367@Component 368struct Page1 { 369 build() { 370 Column() { 371 Image($r('app.media.bg2')).width('100%').height('100%') // The image is stored in the media folder. 372 } 373 .onClick(() => { 374 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 375 }) 376 } 377 378 // Use the default effects provided by the system, such as translation, scaling, and opacity. 379 pageTransition() { 380 // 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. 381 PageTransitionEnter({ duration: 1000 }) 382 .slide(SlideEffect.Left) 383 // 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. 384 PageTransitionExit({ duration: 1200 }) 385 .translate({ x: 100.0, y: 100.0 }) 386 .opacity(0) 387 } 388} 389``` 390 391 392 393### Example 2: Setting Translation Effects for Entrance and Exit 394 395Method 1: Configure the various translation effects provided, with the system language layout mode set to right-to-left (RTL). 396 397```ts 398// Index.ets 399@Entry 400@Component 401struct Index { 402 @State scale1: number = 1; 403 @State opacity1: number = 1; 404 405 build() { 406 Column() { 407 Button("Page 1").onClick(() => { 408 this.getUIContext().getRouter().pushUrl({ 409 url: "pages/Page1" 410 }) 411 }) 412 .width(200) 413 .height(60) 414 .fontSize(36) 415 Text("START") 416 .fontSize(36) 417 .textAlign(TextAlign.Center) 418 } 419 .scale({ x: this.scale1 }) 420 .opacity(this.opacity1) 421 .height("100%") 422 .width("100%") 423 .justifyContent(FlexAlign.Center) 424 } 425 426 // Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity. 427 pageTransition() { 428 // Set the entrance animation. 429 PageTransitionEnter({ duration: 200 }) 430 .slide(SlideEffect.START) 431 // Set the exit animation. 432 PageTransitionExit({ delay: 100 }) 433 .slide(SlideEffect.START) //Left 434 } 435} 436``` 437 438```ts 439// Page1.ets 440@Entry 441@Component 442struct Page1 { 443 @State scale1: number = 1; 444 @State opacity1: number = 1; 445 446 build() { 447 Column() { 448 Button("Page 2").onClick(() => { 449 this.getUIContext().getRouter().pushUrl({ 450 url: "pages/Index" 451 }); 452 }) 453 .width(200) 454 .height(60) 455 .fontSize(36) 456 Text("END") 457 .fontSize(36) 458 .textAlign(TextAlign.Center) 459 } 460 .scale({ x: this.scale1 }) 461 .opacity(this.opacity1) 462 .height("100%") 463 .width("100%") 464 .justifyContent(FlexAlign.Center) 465 } 466 467 // Customization method 2: Use the default effects provided by the system, such as translation, scaling, and opacity. 468 pageTransition() { 469 PageTransitionEnter({ duration: 200 }) 470 .slide(SlideEffect.END) //Right 471 PageTransitionExit({ delay: 100 }) 472 .slide(SlideEffect.END) //Right 473 } 474} 475 476``` 477 478 479 480Method 2: Use the system's default entrance and exit effects, with the system language layout mode set to right-to-left (RTL). 481 482```ts 483// Index.ets 484@Entry 485@Component 486struct Index { 487 @State scale1: number = 1; 488 @State opacity1: number = 1; 489 490 build() { 491 Column() { 492 Button("Page 1").onClick(() => { 493 this.getUIContext().getRouter().pushUrl({ 494 url: "pages/Page1" 495 }); 496 }) 497 .width(200) 498 .height(60) 499 .fontSize(36) 500 } 501 .scale({ x: this.scale1 }) 502 .opacity(this.opacity1) 503 .height("100%") 504 .width("100%") 505 .justifyContent(FlexAlign.Center) 506 } 507} 508``` 509 510```ts 511// Page1.ets 512@Entry 513@Component 514struct Page1 { 515 @State scale1: number = 1; 516 @State opacity1: number = 1; 517 518 build() { 519 Column() { 520 Button("Page 2").onClick(() => { 521 this.getUIContext().getRouter().pushUrl({ 522 url: "pages/Index" 523 }); 524 }) 525 .width(200) 526 .height(60) 527 .fontSize(36) 528 } 529 .scale({ x: this.scale1 }) 530 .opacity(this.opacity1) 531 .height("100%") 532 .width("100%") 533 .justifyContent(FlexAlign.Center) 534 } 535} 536``` 537 538 539