Back to Uni App

Create Intersection Observer

docs/api/create-intersection-observer.md

2.3.39.0 KB
Original Source

uni.createIntersectionObserver(component, options) @createintersectionobserver

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

createIntersectionObserver 兼容性

Web微信小程序AndroidiOSHarmonyOS
4.04.41<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

参数

名称类型必填默认值兼容性描述
componentany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
optionsCreateIntersectionObserverOptions-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x

options 的属性描述

名称类型必备默认值兼容性描述
thresholdsArray<any>-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x所有阈值
initialRationumber-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x初始的相交比例
observeAllboolean-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x是否同时观测多个参照节点(而非一个)
nativeModeboolean-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x需要基础库: 3.5.7

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

返回值

类型
IntersectionObserver

IntersectionObserver 的方法 @intersectionobserver-values

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

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

relativeTo 兼容性
Web微信小程序AndroidiOSHarmonyOS
-4.41xxx
参数
名称类型必填默认值兼容性描述
selectorstring-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
marginsany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
返回值
类型
IntersectionObserver

relativeToViewport(margins?: any): IntersectionObserver; @relativetoviewport

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

relativeToViewport 兼容性
Web微信小程序AndroidiOSHarmonyOS
-4.41xxx
参数
名称类型必填默认值兼容性描述
marginsany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
返回值
类型
IntersectionObserver

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

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

observe 兼容性
Web微信小程序AndroidiOSHarmonyOS
-4.41xxx
参数
名称类型必填默认值兼容性描述
targetSelectorstring-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
callback(result: ObserveResult) => void-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
ObserveResult 的属性值 @observeresult-values
名称类型必备默认值兼容性描述
intersectionRationumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x相交比例
intersectionRectany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x相交区域的边界
boundingClientRectObserveNodeRect-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x目标节点布局区域的边界
relativeRectObserveNodeRect-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x参照区域的边界
timenumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x相交检测时的时间戳

boundingClientRect 的属性描述

名称类型必备默认值兼容性描述
leftnumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: xleft
rightnumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: xright
topnumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: xtop
bottomnumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: xbottom

disconnect(): void; @disconnect

disconnect 停止监听

disconnect 兼容性
Web微信小程序AndroidiOSHarmonyOS
-4.41xxx

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/create-intersection-observer/create-intersection-observer

示例

vue
<template>
	<view>
		<page-head :title="data.title"></page-head>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-title uni-common-mt">
				{{data.appear ? '小球出现' : '小球消失'}}
			</view>
			<scroll-view class="scroll-view" :scrollTop="data.scrollTop">
				<view class="scroll-area">
					<text class="notice">向下滚动让小球出现</text>
					<view class="ball"></view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script setup lang="uts">
  let observer: IntersectionObserver | null = null;

  type DataType = {
    appear: boolean;
    title: string;
    testRes: ObserveResult | null;
    scrollTop: number;
  }

  const data = reactive({
    appear: false,
    title: 'intersectionObserver',
    testRes: null,
    scrollTop: 0
  } as DataType)

  onReady(() => {
    observer = uni.createIntersectionObserver(getCurrentInstance()!.proxy!, {});
    observer.relativeTo('.scroll-view').observe('.ball', (res: ObserveResult) => {
      data.testRes = res;
      if (res.intersectionRatio > 0 && !data.appear) {
        data.appear = true;
      } else if (res.intersectionRatio <= 0 && data.appear) {
        data.appear = false;
      }
    })
  })

  onUnload(() => {
    if (observer) {
      observer.disconnect()
    }
  })

  defineExpose({
    data
  })
</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

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息