> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# IntersectionObserver

APISummary
`IntersectionObserver` 观察目标节点与参照区域的相交情况，并考虑祖先节点的裁剪。可以用它检测节点的曝光和反曝光。

## 实例方法

[`IntersectionObserver.relativeTo()`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-relative-to.md)

通过 `#id` 选择参照节点。

[`IntersectionObserver.relativeToViewport()`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-relative-to-viewport.md)

以 `LynxView` 的可视区域为参照。

[`IntersectionObserver.relativeToScreen()`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-relative-to-screen.md)

以屏幕为参照。

[`IntersectionObserver.observe()`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-observe.md)

通过 `#id` 选择目标节点，并注册回调。

[`IntersectionObserver.disconnect()`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-disconnect.md)

停止观察所有目标节点。

## 示例

以下示例在当前 `LynxView` 中观察 `#targetNode` 与 `#refNode` 的相交情况。

```tsx
const observer = lynx.createIntersectionObserver(
  { componentId: '' },
  { thresholds: [0, 0.25, 0.5, 0.75, 1] },
);

observer.relativeTo('#refNode', { left: 10, right: 10 });
observer.observe('#targetNode', (entry) => {
  console.log(entry.intersectionRatio);
});
```

不再需要观察时，调用 `observer.disconnect()`。

## 注意事项

- 在参照节点和目标节点创建后调用 `relativeTo()` 和 `observe()`。
- Android 端横滑 `scroll-view` 的子节点在滑动时无法触发相应回调，需要在子节点上设置 [`flatten`](/next/zh/api/elements/built-in/view.md#flatten) 为 `false` 才能触发回调。

## 兼容性

**Compatibility Table**

**Query:** `lynx-api.intersection-observer.IntersectionObserver`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| IntersectionObserver | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |
| relativeTo | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |
| relativeToViewport | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |
| relativeToScreen | Yes (2.10) | Yes (3.4) | Yes (2.10) | Yes (3.5) | Yes (3.5) | Yes |
| observe | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |
| disconnect | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |

**`IntersectionObserver`**

Observe the intersection status between the target node and the reference node and the target node and the ancestor node. When the intersection status changes, the corresponding callback is triggered. Based on this, you can monitor whether the target node is exposed/de-exposed.

**`relativeTo`**

Specify a reference node. The reference node is specified by id.

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json) · [API Reference](/next/api/lynx-api/intersection-observer/intersection-observer-relative-to)

**`relativeToViewport`**

Specify a LynxView as a reference node.

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json) · [API Reference](/next/api/lynx-api/intersection-observer/intersection-observer-relative-to-viewport)

**`relativeToScreen`**

Specify the screen as the reference node.

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json) · [API Reference](/next/api/lynx-api/intersection-observer/intersection-observer-relative-to-screen)

**`observe`**

Specify the target node and callback, and the target node is specified according to id.

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json) · [API Reference](/next/api/lynx-api/intersection-observer/intersection-observer-observe)

**`disconnect`**

Clear the target node and callback. The target node will no longer be observed and the corresponding callback will not be triggered.

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/intersection-observer/IntersectionObserver.json) · [API Reference](/next/api/lynx-api/intersection-observer/intersection-observer-disconnect)

