IntersectionObserver.disconnect
2020-08-22 10:40 更新
与页面显示区域的相交区域并不准确代表用户可见的区域,因为参与计算的区域是“布局区域”,布局区域可能会在绘制时被其他节点裁剪隐藏(如遇祖先节点中 overflow 样式为 hidden 的节点)或遮盖(如遇 fixed 定位的节点)。
解释:停止监听,回调函数将不再触发。
方法参数
无
示例
图片示例
代码示例
<view class="wrap">
<scroll-view class="scroll-view" scroll-y>
<view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}">
<text class="notice">向下滚动让小球出现</text>
<!-- 占位元素 -->
<view class="filling"></view>
<!-- 小球 -->
<view class="ball"></view>
</view>
</scroll-view>
<view class="list-area border-bottom">
<view class="list-item-key-4">top:</view>
<view class="list-item-value">{{intersectionRect.top}}</view>
</view>
<view class="list-area border-bottom">
<view class="list-item-key-4">right:</view>
<view class="list-item-value">{{intersectionRect.right}}</view>
</view>
<view class="list-area border-bottom">
<view class="list-item-key-4">bottom:</view>
<view class="list-item-value">{{intersectionRect.bottom}}</view>
</view>
<view class="list-area border-bottom">
<view class="list-item-key-4">left:</view>
<view class="list-item-value">{{intersectionRect.left}}</view>
</view>
<view class="list-area border-bottom">
<view class="list-item-key-4">width</view>
<view class="list-item-value">{{intersectionRect.width}}</view>
</view>
<view class="list-area border-bottom">
<view class="list-item-key-4">height</view>
<view class="list-item-value">{{intersectionRect.height}}</view>
</view>
<button type="primary" bindtap="disconnect">停止监听</button>
</view>
Page({
data: {
intersectionRect: ''
},
onReady() {
this.intersectionObserver = swan.createIntersectionObserver(this);
this.intersectionObserver
.relativeTo('.scroll-view')
.observe('.ball', res => {
this.setData('intersectionRect', res.intersectionRect);
console.log(res.intersectionRect.left); // 相交区域的左边界坐标
console.log(res.intersectionRect.right); // 相交区域的右边界坐标
console.log(res.intersectionRect.top); // 相交区域的上边界坐标
console.log(res.intersectionRect.bottom); // 相交区域的下边界坐标
console.log(res.intersectionRect.width); // 相交区域的宽度
console.log(res.intersectionRect.height); // 相交区域的高度
});
},
disconnect() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
以上内容是否对您有帮助:
更多建议: