wx.createIntersectionObserver()
发布于 5 年前 作者 jiaxiulan 9394 次浏览 来自 问答
  • 需求的场景描述(希望解决的问题)
Page({
  onLoad() {
    wx.createIntersectionObserver().relativeToViewport({bottom: 100}).observe('.target-class', (res) => {
      res.intersectionRatio // 相交区域占目标节点的布局区域的比例
      res.intersectionRect // 相交区域
      res.intersectionRect.left // 相交区域的左边界坐标
      res.intersectionRect.top // 相交区域的上边界坐标
      res.intersectionRect.width // 相交区域的宽度
      res.intersectionRect.height // 相交区域的高度
    })
  }
})

这段代码只有在第一次进入的时候执行,第二次重新进入就不执行了。

很奇怪。求教一下。

  • 希望提供的能力
2 回复

第二次是怎么进入的页面?是不是没有触发onLoad事件?

onReady:function(){

var a = this;

console.log(‘observer:’+observer);

var observer = wx.createIntersectionObserver();

console.log(‘observer:’ + observer);

console.log(‘showRecommend:’ + a.data.showRecommend);

observer.relativeToViewport().observe(’.applyBar’, (res) => {

console.log(‘res.intersectionRatio:’ + res.intersectionRatio);

if (res.intersectionRatio > 0 && a.data.showRecommend == 0) {

a.GetRecommendList();

}

});

},

希望帮忙看看。我刚做小程序没多久,新手。谢谢。

回到顶部