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, and this page is available as Markdown at /next/zh/api/lynx-api/lynx/lynx-create-intersection-observer.md.
Lynx
  • 简体中文
  • lynx: createIntersectionObserver() static method

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

    语法

    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 对象。

    示例

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

    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()。

    兼容性

    LCD tables only load in the browser

    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。