Marquee组件在文本末尾滚动到控件末尾时,能停止滚动并触发回调事件(目前是滚动到控件的开头)

文字轮播卡片,需要在文字滚动到控件末尾的时候,停止滚动,并等待其他文字轮播的动画停止,当所有的轮播动画都到达控件的末尾时,同时切换文字内容,进行下一轮的轮播。

HarmonyOS
2024-10-11 10:28:02
1095浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以使用scroll自行封装的替代方案,参考下面demo:

@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  @State textList: string[] = [  
    'this is a test string1 this is a test string1 this is a test string1',  
    'this is a test string2 this is a test string2',  
    'this is a test string3 this is a test string3 this is a test string3 this is a test string3',  
  ]  
  
  build() {  
    Row() {  
      Column() {  
        myMarqueeCard({  
          textList: this.textList,  
        })  
      }  
      .width('100%')  
      .margin(20)  
    }  
    .height('100%')  
  }  
}  
  
@Component  
struct myMarqueeCard {  
  @Prop textList: string[]  
  scroller1: Scroller = new Scroller()  
  scroller2: Scroller = new Scroller()  
  scroller3: Scroller = new Scroller()  
  
  build() {  
    Column() {  
      this.SingleText(this.textList[0], this.scroller1)  
      this.SingleText(this.textList[1], this.scroller2)  
      this.SingleText(this.textList[2], this.scroller3)  
    }  
  }  
  
  @Builder  
  SingleText(text: string, scroller: Scroller) {  
    Scroll(scroller) {  
      Row() {  
        Text(text).fontSize(30)  
      }  
    }  
    .width(300)  
    .scrollable(ScrollDirection.Horizontal)  
    .enableScrollInteraction(false)  
    .scrollBar(BarState.Off)  
    .onAppear(() => {  
      this.handleScroll(scroller)  
    })  
  }  
  
  handleScroll(scroller: Scroller) {  
    let timer: number = setInterval(() => {  
      const curOffset: OffsetResult = scroller.currentOffset()  
      scroller.scrollTo({  
        xOffset: curOffset.xOffset + 50, yOffset: curOffset.yOffset, animation: {  
          duration: 1000,  
          curve: Curve.Linear  
        }  
      })  
      if (scroller.isAtEnd()) {  
        clearInterval(timer);  
        if (this.scroller1.isAtEnd() && this.scroller2.isAtEnd() && this.scroller3.isAtEnd()) {  
          // 其他操作  
        }  
      }  
    }, 500)  
  }  
}
  • 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.

Scroll参考: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5

分享
微博
QQ
微信
回复
2024-10-11 17:29:09


相关问题
list组件无法滚动到底部
2338浏览 • 1回复 待解决
HarmonyOS List组件默认滚动到最底部
1204浏览 • 1回复 待解决
Web组件怎么知道滚动到顶部了
1362浏览 • 1回复 待解决
怎么判断webview滚动到最下方?
982浏览 • 2回复 待解决
HarmonyOS List列表滚动到指定位置
1218浏览 • 1回复 待解决
HarmonyOS swiper如何滚动到任意页面?
1132浏览 • 1回复 待解决
HarmonyOS list如何动态滚动到指定位置
880浏览 • 1回复 待解决
HarmonyOS 控件高度随滚动变化
908浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了1人