文档管理中心

Tab组件页面切换时,如何不显示中间过渡的tab页

如果需要做动画,可以通过设置.animationDuration(0)跳过中间过渡页显示,示例代码如下:

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct TABTransitionAnimation {
  4. @State fontColor: string = '#182431';
  5. @State selectedFontColor: string = '#007DFF';
  6. @State currentIndex: number = 0;
  7. private tabController: TabsController = new TabsController();
  8. @Builder
  9. tabBuilder(index: number, name: string) {
  10. Column() {
  11. Text(name)
  12. .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
  13. .fontSize(16)
  14. .fontWeight(this.currentIndex === index ? 500 : 400)
  15. .lineHeight(22)
  16. .margin({
  17. top: 17,
  18. bottom: 7
  19. })
  20. Divider()
  21. .strokeWidth(2)
  22. .color($r('sys.color.brand'))
  23. .opacity(this.currentIndex === index ? 1 : 0)
  24. }
  25. .width('100%')
  26. }
  27. build() {
  28. Column() {
  29. Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.tabController }) {
  30. TabContent() {
  31. Column()
  32. .width('100%')
  33. .height('100%')
  34. .backgroundColor('#00CB87')
  35. }.tabBar(this.tabBuilder(0, 'green'))
  36. TabContent() {
  37. Column()
  38. .width('100%')
  39. .height('100%')
  40. .backgroundColor($r('sys.color.brand'))
  41. }.tabBar(this.tabBuilder(1, 'blue'))
  42. TabContent() {
  43. Column()
  44. .width('100%')
  45. .height('100%')
  46. .backgroundColor($r('sys.color.multi_color_11'))
  47. }.tabBar(this.tabBuilder(2, 'yellow'))
  48. TabContent() {
  49. Column()
  50. .width('100%')
  51. .height('100%')
  52. .backgroundColor('#E67C92')
  53. }
  54. .tabBar(this.tabBuilder(3, 'pink'))
  55. }
  56. .width(360)
  57. .height(296)
  58. .barWidth(360)
  59. .barHeight(56)
  60. .vertical(false)
  61. .barMode(BarMode.Fixed)
  62. .backgroundColor('#F1F3F5')
  63. .margin({ top: 52 })
  64. .animationDuration(0) // Setting the animation time to 0 can solve the problem of switching between pages and displaying intermediate transition pages
  65. .onChange((index: number) => {
  66. if (index >= 0 && index <= 3) {
  67. this.currentIndex = index;
  68. }
  69. })
  70. }
  71. .width('100%')
  72. }
  73. }