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, position: 'static' as Export.Position, observerCls: '__bsl-nav-observer__', height: 0, top: 0 }; class Instance extends C { constructor() { super(state, { }); } public externalClasses = ['wrap-cls']; public options = { styleIsolation: 'apply-shared', // 指定所有 _ 开头的数据字段为纯数据字段 pureDataPattern: /^_/ } public ready() { const intersectionObserver = this.createIntersectionObserver(); const rect = wx.getMenuButtonBoundingClientRect(); intersectionObserver.relativeToViewport({ top: -rect.bottom + rpx(24) }); intersectionObserver.observe('.' + this.data.observerCls, (res) => { this.setData({ position: res.intersectionRatio <= 0 ? 'fixed' : 'static' }); const detail: Export.StickyEventDetail = { position: this.data.position }; this.triggerEvent('sticky', detail); }); this.setData({ height: rect.bottom + rpx(24), top: rect.top, _intersectionObserver: intersectionObserver }); } public detache() { if (this.data._intersectionObserver) { this.data._intersectionObserver.disconnect(); } } } Component(new Instance().init());