W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
與頁面顯示區(qū)域的相交區(qū)域并不準確代表用戶可見的區(qū)域,因為參與計算的區(qū)域是“布局區(qū)域”,布局區(qū)域可能會在繪制時被其他節(jié)點裁剪隱藏(如遇祖先節(jié)點中 overflow 樣式為 hidden 的節(jié)點)或遮蓋(如遇 fixed 定位的節(jié)點)。
解釋:停止監(jiān)聽,回調(diào)函數(shù)將不再觸發(fā)。
無
<view class="wrap">
<scroll-view class="scroll-view" scroll-y>
<view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}">
<text class="notice">向下滾動讓小球出現(xiàn)</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">停止監(jiān)聽</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); // 相交區(qū)域的左邊界坐標
console.log(res.intersectionRect.right); // 相交區(qū)域的右邊界坐標
console.log(res.intersectionRect.top); // 相交區(qū)域的上邊界坐標
console.log(res.intersectionRect.bottom); // 相交區(qū)域的下邊界坐標
console.log(res.intersectionRect.width); // 相交區(qū)域的寬度
console.log(res.intersectionRect.height); // 相交區(qū)域的高度
});
},
disconnect() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: