onLoad() {
this._observer = wx.createIntersectionObserver()
this._observer
//.relativeTo('.scroll-view')
.relativeToViewport() //指定页面显示区域作为参照区域之一
.observe('.ball', (res) => { //在参照区域里监听目标节点是否显示
console.log(res);
if(res.intersectionRatio > 0){
console.log('.ball进入了可视区域')
}else{
console.log('.ball离开了可视区域')
}
})
},
onUnload() {
//停止监听
if (this._observer) this._observer.disconnect()
}
<view class="ball">哈哈哈</view>
创建并返回一个 IntersectionObserver 对象实例。在自定义组件或包含自定义组件的页面中,应使用 this.createIntersectionObserver()
来代替。