HarmonyOS Scroll组件回调事件onScroll\(event: \(xOffset: number, yOffset: number\) =\> void\),返回的参数xOffset/yOffset,数据不对?

如题:HarmonyOS Scroll组件回调事件onScroll(event: (xOffset: number, yOffset: number) =\> void),返回的参数xOffset/yOffset,数据不对?

阅读 566
1 个回答

问题场景

Scroll组件回调事件onScroll(event: (xOffset: number, yOffset: number) =\> void),经测试,返回的xOffset,yOffset数值并不是滚动偏移量,而是滚动速度

调用Scroll组件回调函数onScroll,获取参数yOffset

获取到的数值发现与滚动偏移量无关,且经常为负数,经验证数值为滚动速度

onscroll事件会丢失精度,建议用currentOffset来获取当前的滚动偏移量实现吸顶效果,代码如下:

.onScroll((xOffset: number, yOffset: number) => {
  // console.log(yOffset.toString())
  // this.scrollY += yOffset

  // this.tabPosY = this.topAreaHeight - this.scrollY <= 0 ? 0 : this.topAreaHeight - this.scrollY
  this.tabPosY = this.topAreaHeight - this.scroller.currentOffset()
    .yOffset <= 0 ? 0 : this.topAreaHeight - this.scroller.currentOffset()
    .yOffset
  // console.log(TAG, `Scrollview is scrolling, scrollY: ${this.scrollY}, tabPosY: ${this.tabPosY}`);
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进