uni.createIntersectionObserver(component, options)
创建并返回一个 IntersectionObserver 对象实例
createIntersectionObserver 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| 4.0 | 4.41 | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| component | any | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
| options | CreateIntersectionObserverOptions | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | thresholds | Array<any> | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | x | x | x |
| 所有阈值 | | initialRatio | number | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | x | x | x |
| 初始的相交比例 | | observeAll | boolean | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | x | x | x |
| 是否同时观测多个参照节点(而非一个) | | nativeMode | boolean | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | x | x | x |
| 需要基础库: 3.5.7
是否使用原生观察器模式。
|
|
返回值
IntersectionObserver 的方法
relativeTo(selector: string, margins?: any): IntersectionObserver;
relativeTo
使用选择器指定一个节点,作为参照区域之一
relativeTo 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| - | 4.41 | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| selector | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
| margins | any | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
返回值
relativeToViewport(margins?: any): IntersectionObserver;
relativeToViewport
指定页面显示区域作为参照区域之一
relativeToViewport 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| - | 4.41 | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| margins | any | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
返回值
observe(targetSelector: string, callback: ObserveCallback): void;
observe
指定目标节点并开始监听相交状态变化情况
observe 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| - | 4.41 | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| targetSelector | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
| callback | (result: ObserveResult) => void | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| |
ObserveResult 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| intersectionRatio | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| 相交比例 |
| intersectionRect | any | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| 相交区域的边界 |
| boundingClientRect | ObserveNodeRect | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| 目标节点布局区域的边界 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | left | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| left | | right | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| right | | top | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| top | | bottom | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| bottom |
|
| relativeRect | ObserveNodeRect | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| 参照区域的边界 |
| time | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | - | x | x | x |
| 相交检测时的时间戳 |
disconnect(): void;
disconnect
停止监听
disconnect 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| - | 4.41 | x | x | x |
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例源码如下,请查看 pre > code 标签中的内容
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-title uni-common-mt">
{{appear ? '小球出现' : '小球消失'}}
</view>
<scroll-view class="scroll-view" scroll-y :scrollTop="scrollTop">
<view class="scroll-area">
<text class="notice">向下滚动让小球出现</text>
<view class="ball"></view>
</view>
</scroll-view>
</view>
</view>
</template>
<script lang="uts">
let observer: IntersectionObserver | null = null;
export default {
data() {
return {
appear: false,
title:'intersectionObserver',
// 自动化测试
testRes: null as ObserveResult | null,
scrollTop:0
}
},
onReady() {
observer = uni.createIntersectionObserver(this,{});
observer.relativeTo('.scroll-view').observe('.ball', (res:ObserveResult) => {
this.testRes = res;
if (res.intersectionRatio > 0 && !this.appear) {
this.appear = true;
} else if (res.intersectionRatio <= 0 && this.appear) {
this.appear = false;
}
})
},
onUnload() {
if (observer) {
observer.disconnect()
}
}
}
</script>
<style>
.scroll-view {
height: 200px;
background: #fff;
border: 1px solid #ccc;
box-sizing: border-box;
}
.scroll-area {
height: 650px;
display: flex;
flex-direction: column;
align-items: center;
}
.notice {
margin-top: 75px;
margin:75px 0 200px 0;
}
.ball {
width: 100px;
height: 100px;
background: #4cd964;
border-radius: 100px;
}
</style>
参见
通用类型
GeneralCallbackResult
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | - | - | - |
| 错误信息 |