微信小程序scroll-view滚动scrollTop不准确

小程序里使用scroll-view

<scroll-view style="width: 100%;height: {{windowHeight}}px;background-color:#ececec;" 
				id="scroll-view" 
				scroll-y="true" 
				bindscroll="scroll">
</scroll-view>

bindscroll="scroll"内的scroll方法可检测滚动距离

scroll(e) {
    console.log('滚动距离:'+e.dateil.scrollTop)
}

但是在快读滚动的过程中明明已经滚动到顶部了scrollTop竟然还在300。
缓慢滑动虽然没有问题,但是在快速滚动的时候scrollTop会出现非常大的误差。
以下是解决方法
1.这是因为每次滚动监听事件都会被调用比较耗费资源系统会默认节流可以在scroll-view 加一个 throttle=“{{false}}” 关闭节流

<scroll-view 
	style="width: 100%;height: {{windowHeight}}px;background-color:#ececec;" 
	id="scroll-view" 
	throttle="{{false}}" 
	scroll-y="true" 
	bindscroll="scroll">
</scroll-view>

2.减少scroll方法内的操作因为scroll-view内随便滑动一下监听事件可能会被调用上百次,如果在方法内写this.setData这种赋值的操作小程序可能会卡死无法滑动,建议在scrollTop到达一定数值后在进行操作,比如scrollTop=0先判断时候滑动到顶部或者底部,再来控制其他view的显示隐藏。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值