.nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST });
this.scrollerForScroll.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000 } }); this.scrollerForList.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000 } });
完整示例参考如下:
@Entry @Component struct ListWithinScrollAnimation { private scrollerForScroll: Scroller = new Scroller(); private scrollerForList: Scroller = new Scroller(); private tabController: TabsController = new TabsController(); @State listInfoItem: Array<ResourceStr> = new Array(10).fill($r('app.media.img1')); // 图片资源需自行替换 @State tabBarContent: Array<string> = ['第一页', '第二页', '第三页']; @Builder tabBuilder(item: string) { Column() { Text(item) .width(100) .height(50) .textAlign(TextAlign.Center); }.width('100%'); } @Builder tabContentData() { List({ space: 10, scroller: this.scrollerForList }) { ForEach(this.listInfoItem, (item: ResourceStr) => { ListItem() { Column() { Image(item).width('80%'); Text('我不再迷茫,思念是唯一的行囊,满天的星光,有一颗是你的愿望') .fontSize(14) .margin({ top: 4 }) .maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis }) .fontColor($r('sys.color.ohos_id_color_text_secondary')) .padding(10); Button('立即购买') .height(40) .backgroundColor(Color.Orange) .width('80%') .margin({ bottom: 24 }); } .width('100%') .borderRadius(12) .alignItems(HorizontalAlign.Center) .backgroundColor($r('sys.color.ohos_id_color_background')); }; }); } .scrollBar(BarState.Off) .edgeEffect(EdgeEffect.None) .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }); } build() { Stack({ alignContent: Alignment.Center }) { Scroll(this.scrollerForScroll) { Column() { Swiper() { ForEach(this.listInfoItem, () => { Image($r('app.media.img2')); // 图片资源需自行替换 }); } .height(250) .loop(true) .autoPlay(true) .interval(1000); Tabs({ barPosition: BarPosition.Start, index: 0, controller: this.tabController }) { ForEach(this.tabBarContent, (item: string) => { TabContent() { this.tabContentData(); }.tabBar(this.tabBuilder(item)); }); }.vertical(false).barWidth('100%'); }; }.scrollBar(BarState.Off); Button('back top') .height('5%') .onClick(() => { this.scrollerForScroll.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000 } }); this.scrollerForList.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000 } }); }); }; } }