小程序setdata优化_小程序长列表渲染优化另一种解决方案 (二)

0290b494793723c73159ad31adc36e06.png

前言

上面这篇文章

daisy:小程序长列表渲染优化另一种解决方案​zhuanlan.zhihu.com
5e40e58f8fe49da3f763084566ea2972.png

,我们通过监听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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值