import C from 'bsl-mp/app/Component'; import { rpx } from 'bsl-mp/utils'; import * as Export from './export'; const state = { _intersectionObserver: null as WechatMiniprogram.IntersectionObserver | null }; const systemInfo = wx.getSystemInfoSync(); class Instance extends C { constructor() { super(state, { offset: Number }); } public options = { // 指定所有 _ 开头的数据字段为纯数据字段 pureDataPattern: /^_/ }; public watch = { offset() { const self = this as unknown as Instance; if (self.data._intersectionObserver) { self.data._intersectionObserver.disconnect(); } self.observer(); } } public ready() { this.observer(); } public observer() { if (!this.data._intersectionObserver) { this.setData({ _intersectionObserver: this.createIntersectionObserver() }); } const intersectionObserver = this.data._intersectionObserver!; intersectionObserver.relativeToViewport({ bottom: this.properties.offset }).observe('.root', (res) => { if (systemInfo.screenHeight - res.intersectionRect.top > 50 && res.intersectionRatio >= 1) { this.triggerEvent('show', {}); } else if (res.intersectionRatio <= 0) { this.triggerEvent('hide', {}); } }); } public detache() { if (this.data._intersectionObserver) { this.data._intersectionObserver.disconnect(); } } } Component(new Instance().init());