> 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.

# lynx: createIntersectionObserver() static method

APISummary
`lynx.createIntersectionObserver()` 创建一个 `IntersectionObserver`。通过 `options` 配置回调条件。

## 语法

```ts
createIntersectionObserver(
  component: { componentId: string },
  options?: {
    thresholds?: number[];
    initialRatio?: number;
    observeAll?: boolean;
  },
): IntersectionObserver;
```

### 参数

#### component

传 `{ componentId: '' }` 在当前 `LynxView` 中查找目标节点和参照节点。指定组件 ID 时，优先在该组件内查找，未找到时再查找整个 `LynxView`。

#### options

- `thresholds` 是 `[0, 1]` 范围内的相交比例阈值列表，默认 `[0]`。后续检测中，相交比例达到、离开或跨过任一阈值时触发回调。
- `initialRatio` 是首次检测的比较值，范围为 `[0, 1]`，默认 `0`。默认仅在首次相交比例大于 `initialRatio` 时触发回调。开启页面开关 `enableIntersectionObserverInitialRatioCompatibility`（Lynx 4.5 起支持）后，首次相交比例必须不同于 `initialRatio`，且大于或等于至少一个配置阈值。
- `observeAll` 在 Android、iOS 和 Harmony 上为保留选项，默认 `false`。

### 返回值

[IntersectionObserver 对象](/next/zh/api/lynx-api/intersection-observer.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()`。

## 兼容性

**Compatibility Table**

**Query:** `lynx-api.lynx.createIntersectionObserver`

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

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| createIntersectionObserver | Yes (1.5) | Yes (3.4) | Yes (1.5) | Yes (3.5) | Yes (3.5) | Yes |

**`createIntersectionObserver`**

Create an IntersectionObserver object.

