![0290b494793723c73159ad31adc36e06.png](https://i-blog.csdnimg.cn/blog_migrate/35734ccbc1ae3577c5b704deac26c77f.jpeg)
前言
上面这篇文章
daisy:小程序长列表渲染优化另一种解决方案zhuanlan.zhihu.com![5e40e58f8fe49da3f763084566ea2972.png](https://i-blog.csdnimg.cn/blog_migrate/3a89baaea155528dc04e637c4ef3c94d.jpeg)
,我们通过监听Scroll事件来计算当前应该渲染哪一屏,但是我们有observeAPI啊,为何要那么麻烦,所以这篇文章,我们在用这个API简化一下。
简化思路
利用observeAPI 在每一屏渲染完成后,监听当前这一屏是否在可视区域内,如果在就给它赋值正常的数组,否则是{ height: that.pageHeightArr[pageIndex] } 。
这里新增一个observePage 的函数来做这个事情就行。
observePage: function(pageIndex) {
const that = this;
const { hasShowAlreadySaw, showMoreVideosIcon } = this.data;
const observerObj = wx.createIntersectionObserver(this).relativeToViewport({ top: 2 * this.windowHeight, bottom: 2 * this.windowHeight });
observerObj.observe(`#wrp_${pageIndex}`, (res) => {
if(res.intersectionRatio <= 0) {
that.setData({
['list[' + pageIndex + ']']: { height: that.pageHeightArr[pageIndex] } ,
})
} else {
that.setData({
['list[' + pageIndex + ']']: that.wholeVideoList[pageIndex] ,
})
}
});
},
这样我们就可以把原来onPageScroll的东西删除了,完整代码如下:
https://developers.weixin.qq.com/s/gt4kuZmp73hL