W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋: 創(chuàng)建并返回一個(gè) IntersectionObserver 對(duì)象實(shí)例。在自定義組件中,可以使用 this.createIntersectionObserver([options]) 來(lái)代替。
Object object
屬性名 | 類型 | 必填 | 默認(rèn)值 | 說(shuō)明 | ||
---|---|---|---|---|---|---|
thresholds |
Array |
否 |
[0] |
一個(gè)數(shù)值數(shù)組,包含所有閾值。 |
||
initialRatio |
number |
否 |
0 |
初始的相交比例,如果調(diào)用時(shí)檢測(cè)到的相交比例與這個(gè)值不相等且達(dá)到閾值,則會(huì)觸發(fā)一次監(jiān)聽(tīng)器的回調(diào)函數(shù)。 |
||
selectAll |
Boolean |
否 |
false |
是否同時(shí)觀測(cè)多個(gè)目標(biāo)節(jié)點(diǎn)(而非一個(gè)),如果設(shè)為 true ,observe 的 targetSelector 將選中多個(gè)節(jié)點(diǎn)(注意:同時(shí)選中過(guò)多節(jié)點(diǎn)將影響渲染性能) |
代碼示例 1:
<view class="wrap">
<view class="message">
<text s-if="appear">小球出現(xiàn)</text>
<text s-else>小球消失</text>
</view>
<scroll-view class="scroll-view" scroll-y>
<view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}">
<text class="notice">向下滾動(dòng)讓小球出現(xiàn)</text>
<view class="filling"></view>
<view class="ball"></view>
</view>
</scroll-view>
</view>
Page({
data: {
appear: false
},
onLoad() {
this.intersectionObserver = swan.createIntersectionObserver(this);
// 監(jiān)測(cè) scroll-view 可視區(qū)域和小球元素節(jié)點(diǎn)的相交情況
console.log('swan.createIntersectionObserve的可選值', this.intersectionObserver._options);
this.intersectionObserver
.relativeTo('.scroll-view')
.observe('.ball', res => {
console.log('observe', res);
// boundingClientRect: 目標(biāo)邊界,這里指小球的位置情況,這個(gè)位置是相對(duì)于整個(gè)頁(yè)面的,不是相對(duì)于參照元素的 scroll-view 的
// dataset: 觀察對(duì)象攜帶的數(shù)據(jù)。
// id: 觀察對(duì)象的 id,它與上面的 dataset 多使用于一次觀察多個(gè)對(duì)象的場(chǎng)景,用于區(qū)分不同的對(duì)象。
// intersectionRatio: 相交比例,為 0 時(shí)說(shuō)明兩者不相交。
// intersectionRect: 相交區(qū)域,height 為 0 時(shí)說(shuō)明此時(shí)沒(méi)有相交。
// relativeRect: 參照區(qū)域的邊界,作為參考物,它的值一般是不會(huì)變的。可以對(duì)比它于開(kāi)始相交時(shí)一直沒(méi)變,因?yàn)樗褪且粋€(gè) scroll-view 的組件在頁(yè)面上呈現(xiàn)出的位置信息。
// time: 監(jiān)測(cè)到兩者相交時(shí)的時(shí)間戳
this.setData('appear', res.intersectionRatio > 0);
});
},
onUnload() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
.wrap {
padding-top: 30rpx;
}
.message {
display: flex;
width: 100%;
margin: 50rpx 0;
justify-content: center;
font-family: -apple-system-font, Helvetica Neue,Helvetica,sans-serif;
font-size: 40rpx;
}
.scroll-view {
height: 400rpx;
background: #fff;
}
.scroll-area {
display: flex;
flex-direction: column;
height: 1300rpx;
transition: .5s;
align-items: center;
}
.notice {
margin-top: 150rpx;
}
.filling {
height: 400rpx;
}
.ball {
width: 200rpx;
height: 200rpx;
border-radius: 50%;
background: #38f;
}
代碼示例 2: options 為 thresholds 時(shí)
Page({
data: {
appear: false
},
onReady() {
this.intersectionObserver = swan.createIntersectionObserver(this, {
// 閾值數(shù)量設(shè)置少,避免觸發(fā)過(guò)于頻繁導(dǎo)致性能問(wèn)題
// 通常會(huì)設(shè)置為 1,表示元素要完全展示在頁(yè)面上才會(huì)進(jìn)行記錄
thresholds: [0, 0.5, 1]
});
// 監(jiān)測(cè) scroll-view 可視區(qū)域和小球元素節(jié)點(diǎn)的相交情況
// 配置 thresholds:[1],那么當(dāng)監(jiān)聽(tīng)對(duì)象和參照物相交比例達(dá)到 1 時(shí),會(huì)觸發(fā)監(jiān)聽(tīng)器的回調(diào)函數(shù)
console.log('監(jiān)聽(tīng)實(shí)例', this.intersectionObserver);
this.intersectionObserver
.relativeTo('.scroll-view')
.observe('.ball', res => {
console.log('observe', res);
// intersectionRatio: 相交比例,為 0 時(shí)說(shuō)明兩者不相交。
this.setData('appear', res.intersectionRatio > 0);
});
},
onUnload() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
代碼示例 3: options 為 initialRatio 時(shí)
Page({
data: {
appear: false
},
onReady() {
this.intersectionObserver = swan.createIntersectionObserver(this, {
// 初始相交比例,默認(rèn) 0,達(dá)到 initialRatio 或 thresholds 中的閾值時(shí),回調(diào)被觸發(fā)
initialRatio: 1
});
// 監(jiān)測(cè)scroll-view可視區(qū)域 和 小球元素節(jié)點(diǎn)的相交情況
// 配置了 thresholds:[1],那么當(dāng)監(jiān)聽(tīng)對(duì)象和參照物相交比例達(dá)到 1 時(shí),會(huì)觸發(fā)監(jiān)聽(tīng)器的回調(diào)函數(shù)
this.intersectionObserver
.relativeTo('.scroll-view')
.observe('.ball', res => {
console.log('observe', res);
// intersectionRatio: 相交比例,這里為 0 時(shí)說(shuō)明兩者不相交。
this.setData('appear', res.intersectionRatio > 0);
});
},
onUnload() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
代碼示例 4: options 為 selectAll 時(shí)
Page({
data: {
appear: false
},
onReady() {
this.intersectionObserver = swan.createIntersectionObserver(this, {
selectAll: true
});
this.intersectionObserver
.relativeTo('.scroll-view')
.observe('.ball, .ball2', res => {
console.log('observe', res)
this.setData('appear', res.intersectionRatio > 0);
});
},
onUnload() {
this.intersectionObserver && this.intersectionObserver.disconnect();
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: