# uni.createIntersectionObserver(component, options)

创建并返回一个 IntersectionObserver 对象实例

# createIntersectionObserver 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 4.41 x x x

# 参数

名称 类型 必填 默认值 兼容性 描述
component any - - -
options CreateIntersectionObserverOptions - -
名称 类型 必备 默认值 兼容性 描述
thresholds Array<any> -
所有阈值
initialRatio number -
初始的相交比例
observeAll boolean -
是否同时观测多个参照节点(而非一个)
nativeMode boolean -
需要基础库: 3.5.7

是否使用原生观察器模式。

# 返回值

类型
IntersectionObserver

# IntersectionObserver 的方法

# relativeTo(selector: string, margins?: any): IntersectionObserver;

使用选择器指定一个节点,作为参照区域之一

# relativeTo 兼容性
Web 微信小程序 Android iOS HarmonyOS
- 4.41 - - -
# 参数
名称 类型 必填 默认值 兼容性 描述
selector string - - -
margins any - - -
# 返回值
类型
IntersectionObserver

# relativeToViewport(margins?: any): IntersectionObserver;

指定页面显示区域作为参照区域之一

# relativeToViewport 兼容性
Web 微信小程序 Android iOS HarmonyOS
- 4.41 - - -
# 参数
名称 类型 必填 默认值 兼容性 描述
margins any - - -
# 返回值
类型
IntersectionObserver

# observe(targetSelector: string, callback: ObserveCallback): void;

指定目标节点并开始监听相交状态变化情况

# observe 兼容性
Web 微信小程序 Android iOS HarmonyOS
- 4.41 - - -
# 参数
名称 类型 必填 默认值 兼容性 描述
targetSelector string - - -
callback (result: ObserveResult) => void - - -
# ObserveResult 的属性值
名称 类型 必备 默认值 兼容性 描述
intersectionRatio number - - 相交比例
intersectionRect any - - 相交区域的边界
boundingClientRect ObserveNodeRect - - 目标节点布局区域的边界
名称 类型 必备 默认值 兼容性 描述
left number - - left
right number - - right
top number - - top
bottom number - - bottom
relativeRect ObserveNodeRect - - 参照区域的边界
time number - - 相交检测时的时间戳

# disconnect(): void;

停止监听

# disconnect 兼容性
Web 微信小程序 Android iOS HarmonyOS
- 4.41 - - -

# 示例

hello uni-app x

Template

Script

<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>
				<view class="scroll-area">
					<text class="notice">向下滚动让小球出现</text>
					<view class="ball"></view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<style>
	.scroll-view {
		height: 400rpx;
		background: #fff;
		border: 1px solid #ccc;
		box-sizing: border-box;
	}

	.scroll-area {
		height: 1300rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.notice {
		margin-top: 150rpx;
		margin: 150rpx 0 400rpx 0;
	}

	.ball {
		width: 200rpx;
		height: 200rpx;
		background: #4cd964;
		border-radius: 100px;
	}
</style>

# 参见

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息