关于在watch中监控scrollY(new)的问题
来源:5-6 listview 基础组件的开发和应用-右侧快速入口实现(2)
milaiduoduo
2017-11-23
scrollY(newY){ const listHeight = this.listHeight; if (newY > 0) { this.currentIndex = 0; return; } for (let i = 0; i < listHeight.length - 1; i++) { let height1 = listHeight[i]; let height2 = listHeight[i + 1]; if (-newY >= height1 && -newY < height2) { this.currentIndex = i; return; } } this.currentIndex = listHeight.length - 2; }
以上这段可以不用watch来监控,因为这段完全可以写在 scroll暴露出来的scroll事件中,如下。所以说,这只是写法不同,还是写在watch中,另有原因?
scroll(pos){ this.scrollY = pos.y; this._getNewIndexInleftList(); }, _getNewIndexInleftList(){ const listHeight = this.listHeight; let newY = this.scrollY; if (newY > 0) { this.currentIndex = 0; return; } for (let i = 0; i < listHeight.length - 1; i++) { let height1 = listHeight[i]; let height2 = listHeight[i + 1]; if (-newY >= height1 && -newY < height2) { this.currentIndex = i; return; } } this.currentIndex = listHeight.length - 2; }
写回答
1回答
-
ustbhuangyi
2017-11-23
scollY 的改动并不只是在滚动的时候,点右侧列表也会有,而 scrollY 的改变会影响视图,所以从数据驱动的角度来说, watch 它的变化更合理
00
相似问题