HarmonyOS NavDestination\(\)\{\}里使用Scroll组件,执行钩子onShown\(\)\{ scrollTo\(\{xOffset: 0, yOffset: 200\}\) \}滚动到特定位置不生效?

如题:HarmonyOS NavDestination(){}里使用Scroll组件,执行钩子onShown(){ scrollTo({xOffset: 0, yOffset: 200}) }滚动到特定位置不生效?

阅读 599
1 个回答

可以使用onAppear,参考示例如下:

@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Scroll(this.scroller) {
        Column() {
          ForEach(this.arr, (item: number) => {
            Text(item.toString())
              .width('90%')
              .height(150)
              .backgroundColor(0xFFFFFF)
              .borderRadius(15)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .margin({ top: 10 })
          }, (item: string) => item)
        }.width('100%')
      }
      .scrollable(ScrollDirection.Vertical) // 滚动方向纵向
      .scrollBar(BarState.On) // 滚动条常驻显示
      .scrollBarColor(Color.Gray) // 滚动条颜色
      .scrollBarWidth(10) // 滚动条宽度
      .friction(0.6)
      .edgeEffect(EdgeEffect.None)
      .onAppear(() => {
        this.scroller.scrollBy(0, 300)
      })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进