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

# 交叉观察器

交叉观察器提供了一种观察目标节点与参照节点及目标节点与祖先节点之间交叉状态的方法，当交叉状态发生变化时，触发相应回调。

开发者可以通过以下三个步骤来观察目标节点与参照节点交叉状态的变化：

1. 调用 [`lynx.createIntersectionObserver`](/next/zh/api/lynx-api/lynx/lynx-create-intersection-observer.md) 创建 [`IntersectionObserver`](/next/zh/api/lynx-api/intersection-observer.md) 对象，指定交叉状态变化的阈值列表。
2. 调用 [`IntersectionObserver`](/next/zh/api/lynx-api/intersection-observer.md) 对象的 [`relativeTo`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-relative-to.md) 方法指定参照节点。
3. 调用 [`IntersectionObserver`](/next/zh/api/lynx-api/intersection-observer.md) 对象的 [`observe`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-observe.md) 方法指定目标节点与回调。
4. 调用 [`IntersectionObserver`](/next/zh/api/lynx-api/intersection-observer.md) 对象的 [`disconnect`](/next/zh/api/lynx-api/intersection-observer/intersection-observer-disconnect.md) 方法清除目标节点与回调。

在下面这个例子中，开发者监听父节点与子节点是否交叉，并在交叉时输出交叉的子节点 [`id`](/next/zh/api/elements/built-in/view.md#id) 和交叉的位置。

**下面是一个示例: event**

**Entry:** `src/visibility_intersection`
**Bundle:** `dist/visibility_intersection.lynx.bundle` | Web: `dist/visibility_intersection.web.bundle`

```tsx {8-24}
import { root, useState } from "@lynx-js/react";
import type { IntersectionObserver, ObserveCallbackResult, TouchEvent } from "@lynx-js/types";

export default function App() {
  const [eventLog, setEventLog] = useState<string>("");
  let observer: IntersectionObserver | null = null;

  function handleTap(e: TouchEvent) {
    "background only";
    setEventLog("");
    if (observer == null) {
      observer = lynx.createIntersectionObserver({ componentId: "" }, { thresholds: [] });
      observer.relativeTo("#container");
      for (let i = 1; i <= 7; i++) {
        observer.observe("#view-item-" + i, (res: ObserveCallbackResult) => {
          if (res.isIntersecting) {
            let rect = res.intersectionRect;
            let rect_str = ", location: [" + rect.left + ", " + rect.top + ", " + rect.right + ", " + rect.bottom + "]";
            setEventLog((log) => log + (log === "" ? "node: " : "\nnode: ") + res.observerId + rect_str);
          }
        });
      }
    }
  }

  return (
    <view
      style={{ width: "100%", height: "90%", overflow: "hidden" }}
      bindtap={(e) => {
        handleTap(e);
      }}
    >
      <view
        style={{
          width: "calc(100% - 10px)",
          height: "150px",
          margin: "5px",
          borderWidth: "2px",
        }}
      >
        <text>Click the page to get intersected nodes:</text>
        <text style={{ color: "red" }}>{eventLog}</text>
      </view>
      <view
        style={{
          width: "100%",
          height: "calc(100% - 165px)",
          paddingTop: "40px",
          marginTop: "15px",
          backgroundColor: "yellow",
        }}
        id="container"
      >
        {[0, 1, 2, 3, 4, 5, 6].map((item) => {
          return (
            <view
              style={{
                width: "calc(100% - 10px)",
                height: "150px",
                margin: "5px",
                borderRadius: "5px",
                backgroundColor: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${
                  Math.floor(Math.random() * 256)
                })`,
                alignItems: "center",
              }}
              id={`view-item-${item + 1}`}
            >
              <text>view-item-{item + 1}</text>
            </view>
          );
        })}
        <view style={{ position: "absolute" }}>
          <text style={{ color: "blue" }}>view container</text>
        </view>
      </view>
    </view>
  );
}

root.render(<App />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.0



交叉观察器的具体语法可以参考 [`IntersectionObserver`](/next/zh/api/lynx-api/intersection-observer.md)。
