HarmonyOS tabbar悬停效果

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#示例3

Scroll官网的这个例子,如果顶部还有一个类似标题栏的一层,我想把下面的tabbar,悬浮到这个标题栏该怎么实现。

HarmonyOS
2024-09-03 11:00:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

请参考如下demo,是否可以满足需求:

enum ScrollPosition { 
  start, 
  center, 
  end 
} 
 
@Entry 
@Component 
struct Index { 
  @State listPosition: number = ScrollPosition.start 
  @State scrollPosition: number = ScrollPosition.start 
  @State showTitle: boolean = false 
  @State currentYOffset: number = 0 
  @State opacityNumber: number = 1 
  @State currentScrollOffsetInList: number = 0 
  private currentScrollStateInList: ScrollState | null = null 
  @State msg1: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
  @State msg2: string[] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] 
  @State msg3: string[] = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj'] 
  private scrollerForScroll: Scroller = new Scroller() 
  private scrollerForList: Scroller = new Scroller() 
  scrollGetData: number[] = [] 
  private lastOffset: number = 0 
 
  build() { 
    Stack({ alignContent: Alignment.Top }) { 
      Scroll(this.scrollerForScroll) { 
        Column() { 
          Image($r('app.media.startIcon')) 
            .width('100%') 
            .height('40%') 
          Tabs({ barPosition: BarPosition.Start }) { 
            TabContent() { 
              Column() { 
                List({ space: 10, scroller: this.scrollerForList }) { 
                  ForEach(this.msg3, (item: string) => { 
                    ListItem() { 
                      Text('ListItem' + item) 
                        .width('100%') 
                        .height('100%') 
                        .borderRadius(15) 
                        .fontSize(24) 
                        .textAlign(TextAlign.Center) 
                        .backgroundColor(Color.White) 
                    } 
                    .width('100%') 
                    .height(100) 
                  }, (item: string) => item) 
                } 
                  .padding({ left: 10, right: 10 }) 
                  .width('100%') 
                  .edgeEffect(EdgeEffect.None) 
                  .scrollBar(BarState.Off) 
                  .onReachStart(() => { 
                    this.listPosition = ScrollPosition.start 
                  }) 
                  .onReachEnd(() => { 
                    this.listPosition = ScrollPosition.end 
                  }) 
                  .onScrollFrameBegin((offset: number, state: ScrollState) => { 
                    // 滑动到列表中间时 
                    if (!((this.listPosition == ScrollPosition.start && offset < 0) 
                      || (this.listPosition == ScrollPosition.end && offset > 0))) { 
                      this.listPosition = ScrollPosition.center 
                    } 
 
                    // 如果页面已滚动到底部,列表不在顶部或列表有正向偏移量 
                    if (this.scrollPosition == ScrollPosition.end 
                      && (this.listPosition != ScrollPosition.start || offset > 0)) { 
                      return { offsetRemain: offset }; 
                    } else { 
                      this.scrollerForScroll.scrollBy(0, offset) 
                      return { offsetRemain: 0 }; 
                    } 
                  }) 
                  .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
                    this.currentScrollOffsetInList = scrollOffset 
                    this.currentScrollStateInList = scrollState 
                    if (scrollOffset != 0) { 
                      this.lastOffset = scrollOffset 
                    } 
                    //记录打印0前的最后一个值 
                    console.log(this.lastOffset + ' this.lastOffset') 
 
                    console.log(scrollOffset + ''); 
                    (scrollOffset <= 0 && this.lastOffset < 0) ? this.showTitle = true : this.showTitle = false 
                  }) 
 
              } 
 
            }.tabBar('你好') 
 
            TabContent() { 
              Column().width('100%').height('100%').backgroundColor('#007DFF') 
            }.tabBar('好的') 
 
          } 
          .vertical(false) 
          .barMode(BarMode.Fixed) 
          .barWidth(360) 
          .barHeight(56) 
          .width("100%") 
          .height("92%") 
          .backgroundColor('#F1F3F5') 
        } 
      } 
        .scrollBar(BarState.Off) 
        .width('100%') 
        .height('100%') 
        .onScroll((xOffset: number, yOffset: number) => { 
          this.currentYOffset = this.scrollerForScroll.currentOffset().yOffset 
          console.log("this.currentYOffset => " + this.currentYOffset) 
          let opacityNum = 1 - this.currentYOffset / 249 
          if(opacityNum > 0){ 
            this.opacityNumber = opacityNum 
          } 
          else { 
            this.opacityNumber = 0 
          } 
          // 非(页面在顶部或页面在底部),则页面在中间 
          if (!((this.scrollPosition == ScrollPosition.start && yOffset < 0) 
            || (this.scrollPosition == ScrollPosition.end && yOffset > 0))) { 
            this.scrollPosition = ScrollPosition.center 
          } 
        }) 
        .onScrollEdge((side: Edge) => { // 滚动到边缘事件回调 
          if (side == Edge.Top) { 
            // 页面在顶部 
            this.scrollPosition = ScrollPosition.start 
          } else if (side == Edge.Bottom) { 
            // 页面在底部 
            this.scrollPosition = ScrollPosition.end 
          } 
        }) 
        .onScrollFrameBegin(offset => { 
          if (this.scrollPosition = ScrollPosition.end) { 
            return { offsetRemain: 0 } 
          } else { 
            return { offsetRemain: offset } 
          } 
        }) 
 
      Stack() { 
        Row() { 
          Text('Title') 
            .fontSize(24) 
        } 
        .justifyContent(FlexAlign.Center) 
      } 
      .opacity(this.opacityNumber) 
      .backgroundColor(Color.Red) 
      .width('100%') 
      .height('8%') 
 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(0xDCDCDC) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 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.
  • 103.
  • 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.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
分享
微博
QQ
微信
回复
2024-09-03 15:45:30


相关问题
ArkUI 中如何设置组件的悬停状态?
2407浏览 • 1回复 待解决
HarmonyOS tabbar跳转问题
542浏览 • 1回复 待解决
HarmonyOS tabbar 区域遮挡问题
531浏览 • 1回复 待解决
HarmonyOS Tabs 组件无法隐藏 tabbar
1387浏览 • 1回复 待解决
HarmonyOStabbar居左显示
577浏览 • 1回复 待解决
HarmonyOS TabContent().tabBar 只能居中吗?
1056浏览 • 1回复 待解决
HarmonyOS TabBar热区事件处理
823浏览 • 1回复 待解决
HarmonyOS 自定义tabbar组件开发
828浏览 • 1回复 待解决