在做页面初始化时, 想ScrollView跳转到指定的位置,结果发现代码执行了,却没有跳到指定的位置,代码如下。
componentDidMount(){
scrollView.scrollTo({x: 0, y: 0, animated: true})
}
经过大量查找各种资料,发现在ScrollView初始化时有个动画效果,只有动画结束了,调用scrollView.scrollTo才有效果,官方资料没有说明这点
我们可以这样做,在componentDidMount()里加一个延时操作就可以了。其实Flatlist也有同样的问题。解决办法一样
componentDidMount() { this.fristTime = setTimeout(() => { ScrollView.scrollTo({x: 0, y: 0, animated: true}) }, 0) }